C#, XML, adding new nodes
Asked Answered
D

2

8

I am trying to add new nodes to an existing XML file. i have this file with first test elements in it:

 <?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://prpa.org/XMLSchema1.xsd">
  <studenti>
    <student>
      <ime>test</ime>
      <prezime>test</prezime>
      <ocijena>0</ocijena>
    </student>
  </studenti>
  <profesori>
    <profesor>
      <ime>test</ime>
      <prezime>test</prezime>
    </profesor>
  </profesori>
</Root>

I used this schema to generate this XML document

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://prpa.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://prpa.org/XMLSchema1.xsd"
    xmlns:mstns="http://prpa.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
           >
  <xs:element name='Root'>
    <xs:complexType>
      <xs:sequence>

  <xs:element name="studenti">
    <xs:complexType>
      <xs:sequence>       
   <xs:element name="student">
     <xs:complexType>
      <xs:sequence>
        <xs:element name="ime" type="xs:string"/>
        <xs:element name="prezime" type="xs:string"/>
        <xs:element name="ocijena" type="xs:int"/>
     </xs:sequence>
    </xs:complexType>
  </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="profesori">
    <xs:complexType>
      <xs:sequence>
  <xs:element name="profesor">
      <xs:complexType>
        <xs:sequence>
         <xs:element name="ime" type="xs:string"/>
         <xs:element name="prezime" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
  </xs:sequence>
  </xs:complexType>
  </xs:element>

      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

Now, I need to add new node

 <profesor>
      <ime>test2</ime>
      <prezime>test2</prezime>
    </profesor>

I have tried this so far:

XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("data/sve.xml"));
        XmlNode root = xmldoc.SelectSingleNode("root/profesori", null);

            XmlNode prof = xmldoc.CreateNode(XmlNodeType.Element, "profesor", null);

            XmlNode ime = xmldoc.CreateNode(XmlNodeType.Element, "ime", null);
            ime.InnerText = name;
            prof.AppendChild(ime);

            XmlNode prezime = xmldoc.CreateNode(XmlNodeType.Element, "prezime", null);
            prezime.InnerText = surname;
            prof.AppendChild(prezime);

             root.AppendChild(prof);

            xmldoc.Save(Server.MapPath("data/sve.xml"));

I also tried adding namespace menager to it:

XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmldoc.NameTable);
                nsMgr.AddNamespace("ns", xmldoc.NamespaceURI);
XmlNode root = xmldoc.SelectSingleNode("/ns:root/ns:profesori", nsMgr);

and still i Cant select parent node and add new child node to it. In debug mode "root" object is null with or without namespace so in the end I off course get an null pointer exception.

What am i doing wrong?

P.S. Schemas, namespaces, xml file are all local and writen by me, if that makes any difference...

Dogvane answered 10/2, 2013 at 14:41 Comment(0)
B
15

Your first problem is that the node names in your XPath don't match those of the XML. XML is case sensitive, so you need to use Root, not root:

XmlNode root = xmldoc.SelectSingleNode("/ns:Root/ns:profesori", nsMgr);

Next, instead of xmldoc.NamespaceURI, use the actual namespace uri:

string strNamespace= "http://prpa.org/XMLSchema1.xsd";
nsMgr.AddNamespace("ns", strNamespace);

or do this:

string strNamespace= xmldoc.DocumentElement.NamespaceURI;
nsMgr.AddNamespace("ns", strNamespace);

The NamespaceURI of an XmlDocument object will always be an empty string.

And you should also use this namespace when creating your elements:

XmlNode prof = xmldoc.CreateNode(XmlNodeType.Element, "profesor", strNamespace);

XmlNode ime = xmldoc.CreateNode(XmlNodeType.Element, "ime", strNamespace);
ime.InnerText = name;
prof.AppendChild(ime);

XmlNode prezime = xmldoc.CreateNode(XmlNodeType.Element, "prezime", strNamespace);
prezime.InnerText = surname;
prof.AppendChild(prezime);

root.AppendChild(prof);

You might also consider using the CreateElement() method, which would be slightly shorter:

XmlNode prof = xmldoc.CreateElement("profesor", strNamespace);

Or, my preference would be to use an XmlWriter:

using(XmlWriter writer = root.CreateNavigator().AppendChild())
{
    writer.WriteStartElement("profesor", strNamespace);
    writer.WriteElementString("ime", strNamespace, name);
    writer.WriteElementString("prezime", strNamespace, surname);
    writer.WriteEndElement();
}
Brushoff answered 10/2, 2013 at 15:3 Comment(4)
ty for answer, but "root" node remains null. I did tried with whole namespace, and now i implemented your solution with " string strNamespace = xmldoc.DocumentElement.NamespaceURI;", but still null.Dogvane
ill post latest state in new answerDogvane
@Dogvane Another problem is that you're using "ns:root" instead of "ns:Root". XML and XPath are case sensitive.Brushoff
Omg, am i stupid or what :) , case sensitive, jesus. Tnx a lot, so much trouble over this and such a simple solution. Like always when u go in wrong direction... Tnx again....Dogvane
S
-1

I have the same trouble, with root==null, but MSDN helped me.

You need to use // instead of /

XmlNode root = xmldoc.SelectSingleNode("//ns:Root//ns:profesori", nsMgr);
Spearwort answered 31/7, 2020 at 13:29 Comment(1)
My comment was for VisualStudio 2008 IDE, for WindowsMobile-based handheld computers application development. And it worked for me.Spearwort

© 2022 - 2024 — McMap. All rights reserved.