2010-11-04 2 views
1

XML-Добавление узла в 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> 

Мне нужно добавить книгу узел к нему .. Мой код читает как этот

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"); 

Единственное, что это новый узел, который будет записано выглядит

<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> 

Он имеет дополнительные Xmlns = «» и г: отсутствует в узле

Что я делаю неправильно, пожалуйста ...

ответ

5

Вы хотите:

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

и

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

для создания этих узлов в правильных пространствах имен.

+0

Нет, я получаю их в правильном положении .. но я не получаю – Bikram

+0

Я получаю их в правильном положении, но я получаю результат как – Bikram

+0

Привет, Ник, спасибо, он работает сейчас .. но пустой Теперь xmlns переместился на следующий узел заголовков :( – Bikram

Смежные вопросы