0

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">
   <chapter>this is a sample text</chapter>
</ns0:book>

I tried Greco suggestions but it does not work. Creating a specific XML document using namespaces in C#

would appreciate any help!

Thanks

1 Answer 1

0

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");
Sign up to request clarification or add additional context in comments.

1 Comment

what about the namespace 'mybookurl/sample' then? i tried the above but the output is the same as the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.