C #, XML, adding new nodes

I am trying to add new nodes to an existing XML file. I have this file with the first test items 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 create 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 a 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 a namespace 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 cannot select the parent node and add a new child element to it node. In debug mode, the β€œroot” object has a null value with or without a namespace, so in the end I get a null pointer exception.

What am I doing wrong?

PS Schemas, namespaces, xml file are local and are written by me, if that matters ...

+5
source share
1 answer

, node XPath XML. XML , Root, Root:

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

xmldoc.NamespaceURI uri:

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

:

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

NamespaceURI XmlDocument .

:

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

CreateElement(), :

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

, XmlWriter:

using(XmlWriter writer = root.CreateNavigator().AppendChild())
{
    writer.WriteStartElement("profesor", strNamespace);
    writer.WriteElementString("ime", strNamespace, name);
    writer.WriteElementString("prezime", strNamespace, surname);
    writer.WriteEndElement();
}
+9

All Articles