views:

340

answers:

1

Hi,

I have the following xml

<book>
   <chapter>this is a sample text</chapter>
</book>

and need to add a namespace to it to be like the one below

<ns0:book xmlns:ns0="http://mybookurl/sample"&gt;
   <chapter>this is a sample text</chapter>
</ns0:book>

I tried Greco suggestions but it does not work. http://stackoverflow.com/questions/443250/creating-a-specific-xml-document-using-namespaces-in-c

would appreciate any help!

Thanks

A: 

You can do this by loading the Xml into an XmLDocument then finding each node that you want to add ns0 to and setting that XmlNodes's Prefix property to "ns0".

Something like this:

XmlDocument myDoc = new XmlDocument();
myDoc.LoadXml("my_file.xml");

foreach (XmlNode eachBook in myDoc.GetElementsByTagName("book")) {
    eachBook.Prefix = "ns0";
}

myDoc.Save("my_changed_file.xml");
Adam Crossland
what about the namespace 'http://mybookurl/sample' then?i tried the above but the output is the same as the input.
Walid Frihi