Click to See Complete Forum and Search --> : How to create Xml file using c#
kumar_1981
Aug 9th, 2007, 12:07 PM
Hi
i need to create Xml file in following format
<Files>
<FileLocation Id="C:\\DeskTop">
<FileName>test</FileName>
<FileName>test1</FileName>
<FileName>test2</FileName>
<FileName>test3</FileName>
<FileName>test4</FileName>
</FileLocation>
<FileLocation Id="C:\\Documents">
<FileName>test</FileName>
<FileName>test1</FileName>
<FileName>test2</FileName>
<FileName>test3</FileName>
<FileName>test4</FileName>
</FileLocation>
</File>
And i should be able to read this xml file
Please help me in this regard
Thanks
Vinay
nmadd
Aug 9th, 2007, 01:16 PM
In addition to Googling "c# xml" to find tons of examples, take a look at this one: http://vbforums.com/showthread.php?t=478692
It is in VB but it will at least help you get started. Good luck.
BramVandenbon
Aug 10th, 2007, 09:53 PM
Yeah, tons of tutorials ... really a lot, and most of them suck, imho. Fact is that there are a lot of XML-involving classes, and it looks like if you're not used to it, it's a bit confusing to understand which classes are really crucial. (Just think of it : Xml uses 5 different namespaces!)
First rule which I always apply when dealing with XML.
"If you can choose the XML hierarchy yourself, then don't use attributes but only use XML-tags".
But the format you gave DOES include an attribute, namely the Id-attribute of your FileLocatoin. That's too bad, 'cause it prevents you from using XML-Serialization as far as I know. And XML Serialisation is the easiest way to save and load XML.
So XML-Serialisation is out ... that still leaves a couple of different ways to do it. I made you a diagram in attachement of the (inheritance) relation of he 5 most important classes.
XmlNode is at the top and just an abstract class. That's an important thing to know cause this shows that an XmlNode could be just an attribute, but it could as well be an entire file! :)
But in fact you only need 3 of them to accomplish what you want: XmlDocument for the file, XmlElement for the tags and XmlAttribute for that Id of your FileLocation-tag.
Let's do it
First add the System.Xml namespace.
You will need a document which represents a file. And you will need it to create the tags.
XmlDocument doc = new XmlDocument();
Remember to add the tag after creating it.
XmlElement filesTag = doc.CreateElement("Files");
doc.AppendChild(filesTag);
XmlElement fileLocationTag = doc.CreateElement("FileLocaton");
filesTag.AppendChild(fileLocationTag);
Creating and adding the attribute...
XmlAttribute IdAttribute = doc.CreateAttribute("Id");
IdAttribute.Value = @"C:\DeskTop";
fileLocationTag.Attributes.Append(IdAttribute);
Creation of your 4 child elements ...
for (int i = 1; i <= 4; i++)
{
XmlElement fileNameTag = doc.CreateElement("FileName");
fileLocationTag.AppendChild(fileNameTag);
fileNameTag.InnerText = String.Format("test{0}", i);
}
And saving it ...
doc.Save(@"c:\somefile.txt");
I think with this information you know enough to find how to load the files yourself :).
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.