C#, XML, adding new nodes

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


Solution 1:

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();
}