Writing to existing XML file

R

Rookie

This is my xml-file

<?xml version="1.0" ?>
<call number="123" description="test call">
<def art="12" description="test" status="fixed"/>
</case>

I need to write to this existing xml file and add a new element e.g. <def
art="09" description="test_2" status="new"/>

<?xml version="1.0" ?>
<call number="123" description="test call">
<def art="12" description="test" status="fixed"/>
<def art="09" description="test_2" status="new"/>
</case>

How to do that ?

Rookie
 
M

Martin Honnen

Rookie said:
This is my xml-file

<?xml version="1.0" ?>
<call number="123" description="test call">
<def art="12" description="test" status="fixed"/>
</case>

I need to write to this existing xml file and add a new element e.g. <def
art="09" description="test_2" status="new"/>

<?xml version="1.0" ?>
<call number="123" description="test call">
<def art="12" description="test" status="fixed"/>
<def art="09" description="test_2" status="new"/>
</case>

How to do that ?

With .NET 3.5 you can use LINQ to XML
(http://msdn.microsoft.com/en-us/library/bb387098.aspx) as follows:

XDocument doc = XDocument.Load("file.xml");
doc.Root.Add(
new XElement("def",
new XAtttribute("art", "09"),
new XAttribute("description", "test_2"),
new XAttribute("status", "new")
)
);
doc.Save("file.xml");
 
A

Arne Vajhøj

This is my xml-file

<?xml version="1.0" ?>
<call number="123" description="test call">
<def art="12" description="test" status="fixed"/>
</case>

I need to write to this existing xml file and add a new element e.g.<def
art="09" description="test_2" status="new"/>

<?xml version="1.0" ?>
<call number="123" description="test call">
<def art="12" description="test" status="fixed"/>
<def art="09" description="test_2" status="new"/>
</case>

How to do that ?

Best advice: don't use XML for this but use a database
instead.

Second best advice: read the entire XML file into memory,
modify it and write it back.

Traditional:

using System;
using System.Xml;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\before.xml");
XmlElement elm = doc.CreateElement("def");
elm.SetAttribute("art", "09");
elm.SetAttribute("description", "test_2");
elm.SetAttribute("status", "new");
doc.DocumentElement.AppendChild(elm);
doc.Save(@"C:\after.xml");
}
}
}

LINQ:

using System;
using System.Xml.Linq;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"C:\before.xml");
doc.Root.Add(new XElement("def", new XAttribute("art",
"09"), new XAttribute("description", "test_2"), new XAttribute("status",
"new")));
doc.Save(@"C:\after.xml");
}
}
}

Worst advice: overwrite the end of the file with the desired
content.

using System;
using System.IO;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
Stream stm = new FileStream(@"C:\hack.xml", FileMode.Open,
FileAccess.ReadWrite);
stm.Seek(-9, SeekOrigin.End); // skip "</call>\r\n"
StreamWriter sw = new StreamWriter(stm);
sw.WriteLine(@" <def art=""09"" description=""test_2""
status=""new""/>");
sw.WriteLine("</call>");
sw.Close();
stm.Close();
}
}
}

Arne
 
A

Andy O'Neill

Best advice: don't use XML for this but use a database
instead.

Second best advice: read the entire XML file into memory,
modify it and write it back.

Arne gives good advice.

I would suggest another option if you want to stick with xml.
If you have the option to change the format of your xml.
Work in a dataset and save this as xml.
Your linq will be a lot easier to understand if you flatten the data out and
use a datatable instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top