Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
VB
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 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:

XML
<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>
Posted
Updated 29-Dec-14 11:19am
v2
Comments
PIEBALDconsult 29-Dec-14 17:24pm    
The same way?
Personally I never use xDocument; I use an XmlDocument or an XmlWriter.

1 solution

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


}


}
 
Share this answer
 
v2
Comments
Member 10525430 31-Dec-14 2:56am    
I wrote this code its work fine but if xml file exist the code didnt do anything?
i clean up the xml file's code then i try its not work:(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900