Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 508
klo Avatar asked Feb 10 '13 14:02

klo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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();
}
like image 89
JLRishe Avatar answered Sep 20 '22 14:09

JLRishe