将元素和属性附加到XML文件 [英] Append elements and attributes to an XML file
本文介绍了将元素和属性附加到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML文件,如下所示
<? xml version = 1.0 ? >
< 设备 >
< 设备 名称 = DA-001 >
< plant > 巴西< / plant >
< / equipment >
< 设备 名称 = VERIGY-93K_PSL-032 >
< 工厂 > 巴西< / plant >
< / equipment >
< span class =code-keyword>< equipment 名称 = BA-1500-002 >
< 工厂 > Muar < / plant >
< / equipment >
< / devices >
我需要通过一些C#代码将项目附加到此XML,但是我无法附加并保留相同的XML格式
XmlDocument doc = new XmlDocument();
doc.Load(history);
XmlNode equipment = doc.CreateElement( equipment);
XmlAttribute name = doc.CreateAttribute( name);
name.Value = textBox.Text;
equipment.Attributes.Append(name);
doc.DocumentElement.AppendChild(设备);
XmlNode plant = doc.CreateElement( plant);
plant.InnerText = cmbPlant.SelectedItem.ToString();
doc.DocumentElement.AppendChild(plant);
doc.Save( Equipment.xml);
以上代码附加如下:请注意工厂应位于设备节点且设备不在以前的XML内容关闭。
<? < span class =code-summarycomment> xml version = 1.0 ? >
< 设备 >
< 设备 名称 = DA-001 >
< 工厂 > 巴西< / plant >
< / equipment >
< 设备 < span class =code-attribute> name = VERIGY-93K_PSL-032 >
< plant > 巴西< / plant >
< / equipment >
< 设备 名称 = BA-1500-002 >
< plant > Muar < / plant >
< / equipment >
< 设备 名称 = This_Equipment_added / >
< plant > USA < / plant >
< / devices >
解决方案
您的问题是您要将'equipment'和'plant'添加为根元素的子元素。 。
doc.DocumentElement.AppendChild(设备);
doc.DocumentElement.AppendChild(plant);
将第二行更改为:
equipment.AppendChild(植物);
I have an XML file as below
<?xml version="1.0"?>
<equipments>
<equipment name="DA-001">
<plant>Brazil</plant>
</equipment>
<equipment name="VERIGY-93K_PSL-032">
<plant>Brazil</plant>
</equipment>
<equipment name="BA-1500-002">
<plant>Muar</plant>
</equipment>
</equipments>
I need to append items to this XML via some C# code, however I cannot append and retaining same XML format
XmlDocument doc = new XmlDocument();
doc.Load(history);
XmlNode equipment = doc.CreateElement("equipment");
XmlAttribute name = doc.CreateAttribute("name");
name.Value = textBox.Text;
equipment.Attributes.Append(name);
doc.DocumentElement.AppendChild(equipment);
XmlNode plant = doc.CreateElement("plant");
plant.InnerText = cmbPlant.SelectedItem.ToString();
doc.DocumentElement.AppendChild(plant);
doc.Save("Equipment.xml");
The above code is appending as follows: Notice that the plant should be in the equipment node and the equipment is not being closed as the previous XML contents.
<?xml version="1.0"?>
<equipments>
<equipment name="DA-001">
<plant>Brazil</plant>
</equipment>
<equipment name="VERIGY-93K_PSL-032">
<plant>Brazil</plant>
</equipment>
<equipment name="BA-1500-002">
<plant>Muar</plant>
</equipment>
<equipment name="This_Equipment_added" />
<plant>USA</plant>
</equipments>
解决方案
Your problem is that you are adding both 'equipment' and 'plant' as child of the root element...
doc.DocumentElement.AppendChild(equipment); doc.DocumentElement.AppendChild(plant);
Change the second line to this:
equipment.AppendChild(plant);
这篇关于将元素和属性附加到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文