如何向 XML 添加新行
How to add new lines to XML
我有这个XML-结构:
<?xml version="1.0" ?>
<server xmlns="exampleServer">
<system-properties>
<property name="prop0" value="null"/>
<property name="prop1" value="true"/>
<property name="prop2" value="false"/>
</system-properties>
</server>
但我不知道如何添加具有两个属性的新 'property' 行。
最好的方法是什么?
我试过:
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;
string nameSpace = root.GetAttribute("xmlns");
XmlNode nn = doc.CreateNode(XmlNodeType.Attribute, "property",
nameSpace);
XmlAttribute attr = doc.CreateAttribute("name");
attr.Value = "prop3";
nn.AppendChild(attr);
attr = doc.CreateAttribute("value");
attr.Value = "value3";
nn.AppendChild(attr);
doc.AppendChild(nn);
doc.Save(path);
但是出现错误'The Node has a wrong type'。
您 运行 遇到的异常是由您指定 XmlNodeType.Attribute
造成的。你真正想要的是 XmlNodeType.Element
.
但是,如果您改用 XmlDocument.CreateElement(String, String)
,您将得到一个 XmlElement
对象,它具有使添加属性更容易的方法:
XmlDocument doc = new XmlDocument();
doc.Load(path);
// Create the new XML element and set attributes
string defaultNameSpace = doc.DocumentElement.GetAttribute("xmlns");
var propertyElement = doc.CreateElement("property", defaultNameSpace);
propertyElement.SetAttribute("name", "prop3");
propertyElement.SetAttribute("value", "value3");
// Find the element to append the new element to
var nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("ns", defaultNameSpace);
var sysPropNode = doc.SelectSingleNode("//ns:system-properties", nsMgr);
sysPropNode.AppendChild(propertyElement);
doc.Save(path);
有关示例,请参阅 this fiddle。
最好用LINQ来XML API.
它自 2007 年起在 .Net Framework 中可用。它更容易,也更强大。
c#
void Main()
{
const string inputFile = @"e:\Temp\Guest.xml";
const string outputFile = @"e:\Temp\Guest_new.xml";
XDocument xdoc = XDocument.Load(inputFile);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
xdoc.Descendants(ns + "system-properties").FirstOrDefault()
.Add(new XElement(ns + "property",
new XAttribute("name", "prop3"),
new XAttribute("value", "true")));
xdoc.Save(outputFile);
}
输出
<?xml version="1.0" encoding="utf-8"?>
<server xmlns="exampleServer">
<system-properties>
<property name="prop0" value="null" />
<property name="prop1" value="true" />
<property name="prop2" value="false" />
<property name="prop3" value="true" />
</system-properties>
</server>
我有这个XML-结构:
<?xml version="1.0" ?>
<server xmlns="exampleServer">
<system-properties>
<property name="prop0" value="null"/>
<property name="prop1" value="true"/>
<property name="prop2" value="false"/>
</system-properties>
</server>
但我不知道如何添加具有两个属性的新 'property' 行。 最好的方法是什么? 我试过:
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;
string nameSpace = root.GetAttribute("xmlns");
XmlNode nn = doc.CreateNode(XmlNodeType.Attribute, "property",
nameSpace);
XmlAttribute attr = doc.CreateAttribute("name");
attr.Value = "prop3";
nn.AppendChild(attr);
attr = doc.CreateAttribute("value");
attr.Value = "value3";
nn.AppendChild(attr);
doc.AppendChild(nn);
doc.Save(path);
但是出现错误'The Node has a wrong type'。
您 运行 遇到的异常是由您指定 XmlNodeType.Attribute
造成的。你真正想要的是 XmlNodeType.Element
.
但是,如果您改用 XmlDocument.CreateElement(String, String)
,您将得到一个 XmlElement
对象,它具有使添加属性更容易的方法:
XmlDocument doc = new XmlDocument();
doc.Load(path);
// Create the new XML element and set attributes
string defaultNameSpace = doc.DocumentElement.GetAttribute("xmlns");
var propertyElement = doc.CreateElement("property", defaultNameSpace);
propertyElement.SetAttribute("name", "prop3");
propertyElement.SetAttribute("value", "value3");
// Find the element to append the new element to
var nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("ns", defaultNameSpace);
var sysPropNode = doc.SelectSingleNode("//ns:system-properties", nsMgr);
sysPropNode.AppendChild(propertyElement);
doc.Save(path);
有关示例,请参阅 this fiddle。
最好用LINQ来XML API.
它自 2007 年起在 .Net Framework 中可用。它更容易,也更强大。
c#
void Main()
{
const string inputFile = @"e:\Temp\Guest.xml";
const string outputFile = @"e:\Temp\Guest_new.xml";
XDocument xdoc = XDocument.Load(inputFile);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
xdoc.Descendants(ns + "system-properties").FirstOrDefault()
.Add(new XElement(ns + "property",
new XAttribute("name", "prop3"),
new XAttribute("value", "true")));
xdoc.Save(outputFile);
}
输出
<?xml version="1.0" encoding="utf-8"?>
<server xmlns="exampleServer">
<system-properties>
<property name="prop0" value="null" />
<property name="prop1" value="true" />
<property name="prop2" value="false" />
<property name="prop3" value="true" />
</system-properties>
</server>