C#、XML、添加新节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14798854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#, XML, adding new nodes
提问by klo
I am trying to add new nodes to an existing XML file. i have this file with first test elements in it:
我正在尝试向现有 XML 文件添加新节点。我有这个文件,其中包含第一个测试元素:
<?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 文档
<?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.
我仍然无法选择父节点并向其添加新的子节点。在调试模式下,“root”对象在有或没有命名空间的情况下都是空的,所以最后我当然会得到一个空指针异常。
What am i doing wrong?
我究竟做错了什么?
P.S. Schemas, namespaces, xml file are all local and writen by me, if that makes any difference...
PS 架构、命名空间、xml 文件都是本地的并且由我编写,如果这有什么不同的话......
采纳答案by JLRishe
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
:
您的第一个问题是 XPath 中的节点名称与 XML 中的节点名称不匹配。XML 区分大小写,因此您需要使用Root
,而不是root
:
XmlNode root = xmldoc.SelectSingleNode("/ns:Root/ns:profesori", nsMgr);
Next, instead of xmldoc.NamespaceURI
, use the actual namespace uri:
接下来,xmldoc.NamespaceURI
使用实际的命名空间 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.
XmlDocument
对象的 NamespaceURI将始终为空字符串。
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:
您也可以考虑使用该CreateElement()
方法,该方法会稍微短一些:
XmlNode prof = xmldoc.CreateElement("profesor", strNamespace);
Or, my preference would be to use an XmlWriter:
或者,我更喜欢使用 XmlWriter:
using(XmlWriter writer = root.CreateNavigator().AppendChild())
{
writer.WriteStartElement("profesor", strNamespace);
writer.WriteElementString("ime", strNamespace, name);
writer.WriteElementString("prezime", strNamespace, surname);
writer.WriteEndElement();
}