2010-11-04 2 views
1

XMLAjout d'un nœud à 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> 

Je dois ajouter un nœud de livre il .. Mon code se lit comme ce

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

La seule chose est le nouveau nœud qui obtient écrit ressemble

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

Il a un xmlns = "supplémentaire" et g: manque dans le nœud

Que dois-je faire le mal ... S'il vous plaît

Répondre

5

Vous voulez:

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

et

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

pour créer ces noeuds dans les espaces de noms corrects.

+0

Non je les obtiens dans la position correcte .. mais je n'obtiens pas le – Bikram

+0

Je les obtiens dans la position correcte, mais j'obtiens le résultat comme – Bikram

+0

Salut Nick Merci beaucoup, cela fonctionne maintenant .. mais le vide xmlns a été déplacé vers le prochain nœud de titres maintenant :( – Bikram