C#,XML,添加新节点 [英] C#, XML, adding new nodes

查看:154
本文介绍了C#,XML,添加新节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将新节点添加到现有的XML文件。
我有第一个测试元素这个文件吧:

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>

我用这个模式来生成这个XML文档

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

我也尝试添加的命名空间MENAGER它:

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

和还我不能选择父节点和新的子节点添加到它。在调试模式根对象为null有或没有命名空间,以便在最后,我关闭过程得到一个空指针异常。

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.

我究竟做错了什么?

P.S。架构,命名空间,XML文件是所有本地和我writen,如果让任何区别...

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

推荐答案

您的第一个问题是,在你的XPath节点名称不匹配的XML的。 XML是大小写敏感的,所以你需要使用,而不是

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

接下来,而非 xmldoc.NamespaceURI ,用实际的名称空间URI:

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

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

或做到这一点:

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

的XmlDocument 对象的的namespaceURI将永远是一个空字符串。

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

您也可以考虑使用的createElement()的方法,这将是稍微短:

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

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

或者,我的preference将使用的XmlWriter:

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

这篇关于C#,XML,添加新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆