如何在Xml代码中添加Xml代码 [英] How Can I Add Xml Code Inside Xml Code

查看:238
本文介绍了如何在Xml代码中添加Xml代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Hi,

XDocument ftpXML = new XDocument(
                     new XDeclaration("1.0", "utf-8", "yes"),
                           new XElement("Device",
                                 new XElement("Name", stringArray[1]),
                                 new XElement("IP", stringArray[2])));

                    xml += ftpXML.ToString();



i将此代码用于xml。



i想做;

i想为我的list创建xml代码。但我该怎么办?

bcs它的create header xml并完成相同的标题1个设备,但我有很多设备及其类似:




i use this code for xml.

i want to do;
i want to create xml code for my list.but how can i do?
bcs its create header xml and finish the same header for 1 device but i have lots of device and its like:

<deviceproperties>
  <device>
   <name>asda</name>
   <ip>192.168.1.1</ip>
  </device>
</deviceproperties>
<deviceproperties>
  <device>
   <name>asda2</name>
   <ip>192.168.1.21</ip>
  </device>
</deviceproperties>

推荐答案

调用以下方法向XML添加详细信息或使用详细信息创建新的xml。



private void InsertTransaction(string DevName,string IPAddress)

{

bool isInserted = false;

try

{

string pathXml = @D: \ myXml.xml;



if(File.Exists(pathXml))

{

XmlDocument xDoc = new XmlDocument();

xDoc.Load(pathXml);

XmlNodeList mainNodes = xDoc.GetElementsByTagName(deviceproperties);



XmlNode mainNode = mainNodes.Item(0);



XmlElement insertTrans = xDoc.CreateElement(Device,mainNode.NamespaceURI) ;

mainNode.InsertAfter(insertTrans,mainNode.LastChild);





XmlElement insertNode = xDoc.CreateElement (name);

insertNode.InnerText = DevName;

insertTrans.InsertAfter(insertNode,insertTrans.LastChild);



insertNode = xDoc.CreateElement(ip);

insertNode.InnerText = IPAddress;

insertTrans.InsertA fter(insertNode,insertTrans.LastChild);



xDoc.Save(pathXml);



}

else

{

XmlTextWriter writer = new XmlTextWriter(pathXml,Encoding.UTF8);

writer.Formatting = Formatting.Indented;

writer.WriteStartDocument();

writer.WriteStartElement(deviceproperties);

writer.WriteStartElement(Device );

writer.WriteElementString(name,DevName);

writer.WriteElementString(ip,IPAddress);

writer。 WriteEndElement();

writer.WriteEndElement();

writer.WriteEndDocument();

writer.Flush();

W riter.Close();



}

}

catch(例外)

$





}





}
Call following method to add details to XML or to Create new xml with Details.

private void InsertTransaction(string DevName, string IPAddress)
{
bool isInserted = false;
try
{
string pathXml = @"D:\myXml.xml";

if (File.Exists(pathXml))
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(pathXml);
XmlNodeList mainNodes=xDoc.GetElementsByTagName("deviceproperties");

XmlNode mainNode = mainNodes.Item(0);

XmlElement insertTrans = xDoc.CreateElement("Device", mainNode.NamespaceURI);
mainNode.InsertAfter(insertTrans, mainNode.LastChild);


XmlElement insertNode = xDoc.CreateElement("name");
insertNode.InnerText = DevName;
insertTrans.InsertAfter(insertNode, insertTrans.LastChild);

insertNode = xDoc.CreateElement("ip");
insertNode.InnerText = IPAddress;
insertTrans.InsertAfter(insertNode, insertTrans.LastChild);

xDoc.Save(pathXml);

}
else
{
XmlTextWriter writer = new XmlTextWriter(pathXml, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("deviceproperties");
writer.WriteStartElement("Device");
writer.WriteElementString("name", DevName);
writer.WriteElementString("ip", IPAddress);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();

}
}
catch (Exception)
{


}


}


这篇关于如何在Xml代码中添加Xml代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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