1

the XML

<bookstore xmlns="http://www.contoso.com/books" 
           xmlns:g="http://www.contoso.com/genre">
  <book g:genre="novel" publicationdate="2010-03-01" ISBN="1-123456-15-0">
    <title>61 Hours</title>
    <author xmlns="http://www.contoso.com/author">
      <first-name>Lee</first-name>
      <last-name>Child</last-name>
    </author>
    <price>6.99</price>
  </book>
<bookstore>

I need to add a book node to it.. My code reads like this

strpath = "C:\\BookStore.xml"; 
XmlDocument doc = new XmlDocument(); 
doc.Load(strpath);
XmlNode root = doc.DocumentElement; 
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable); 
nsMgr.AddNamespace("b", "http://www.contoso.com/books"); 
nsMgr.AddNamespace("g", "http://www.contoso.com/genre"); 
nsMgr.AddNamespace("a", "http://www.contoso.com/author"); 
// Create a Book element and populate its attributes 
System.Xml.XmlElement XmlElementbook = doc.CreateElement("book"); 
//create the three attributes to hold the values 
XmlElementbook.SetAttribute("g:genre";"novel5"); 
XmlElementbook.SetAttribute("publicationdate", "2010-11-03"); 
XmlElementbook.SetAttribute("ISBN", "1-00000-00-00"); 
// Insert the new element into the XML tree 
// Create a new XML element and populate its attributes 
System.Xml.XmlElement myXmlElementTitle = doc.CreateElement("title"); 
myXmlElementTitle.InnerText = "TestBook"; 
// Insert the new element under the node we created 
XmlElementbook.AppendChild(myXmlElementTitle);
System.Xml.XmlElement myXmlElementAuthor = doc.CreateElement("author"); 
myXmlElementAuthor.SetAttribute("xmlns", ("http://www.contoso.com/author")); 
System.Xml.XmlElement myXmlElementFirstname = doc.CreateElement("first-name"); 
myXmlElementFirstname.InnerText = "Bikram"; 
myXmlElementAuthor.AppendChild(myXmlElementFirstname);
System.Xml.XmlElement myXmlElementLastname = doc.CreateElement("last-name"); 
myXmlElementLastname.InnerText = "Mann"; 
myXmlElementAuthor.AppendChild(myXmlElementLastname);
XmlElementbook.AppendChild(myXmlElementAuthor);
// Price 
System.Xml.XmlElement myXmlElementPrice = doc.CreateElement("price"); 
myXmlElementPrice.InnerText = "2.99"; 
// Insert the new element under the node we created 
XmlElementbook.AppendChild(myXmlElementPrice);
//append the whole node to file 
doc.DocumentElement.AppendChild(XmlElementbook);
doc.Save("C:\\BookStore.xml");

The only thing is the New node that gets written looks like

<bookstore xmlns="http://www.contoso.com/books" 
           xmlns:g="http://www.contoso.com/genre">
      <book g:genre="novel" publicationdate="2010-03-01" ISBN="1-123456-15-0">
        <title>61 Hours</title>
        <author xmlns="http://www.contoso.com/author">
          <first-name>Lee</first-name>
          <last-name>Child</last-name>
        </author>
        <price>6.99</price>
      </book>

    ***<book genre="novel5" 
             publicationdate="2010-11-03" 
             ISBN="1-00000-00-00" 
             xmlns="">
     <title>TestBook</title>
     <author xmlns="http://www.contoso.com/author">
       <first-name>Bikram</first-name>
       <last-name>Mann</last-name>
     </author>
     <price>2.99</price>
    </book>***
    <bookstore>

It has an extra XMLNS="" and g: is missing in the node

What Am i doing Wrong Please...

1 Answer 1

5

You want:

System.Xml.XmlElement XmlElementbook =
   doc.CreateElement("book","http://www.contoso.com/books"); 

and

XmlElementbook.SetAttribute("genre","http://www.contoso.com/genre","novel5"); 

to create these nodes in the correct namespaces.

Sign up to request clarification or add additional context in comments.

6 Comments

No I am getting them in correct position .. but i dont get the
I am getting them in correct position , but i get the result as
Hi Nick Thank you so much, it is working now .. but the empty xmlns has moved to the next titles node now :(
@Nick Jones: +1 This is a correct answer. @Bikram: Did you check this? The reset namespace declaration is because you are building an element in the null (or empty) namespace URI.
@Bikram: You should use this method for every element under a not null namespace URI.
|

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.