Results 1 to 3 of 3

Thread: How to create Xml file using c#

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    How to create Xml file using c#

    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
    Last edited by kumar_1981; Aug 9th, 2007 at 12:22 PM.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: How to create Xml file using c#

    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.

  3. #3
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: How to create Xml file using c#

    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.
    Code:
                XmlDocument doc = new XmlDocument();
    Remember to add the tag after creating it.
    Code:
                XmlElement filesTag = doc.CreateElement("Files");
                doc.AppendChild(filesTag);
    
                XmlElement fileLocationTag = doc.CreateElement("FileLocaton");
                filesTag.AppendChild(fileLocationTag);
    Creating and adding the attribute...
    Code:
                XmlAttribute IdAttribute = doc.CreateAttribute("Id");
                IdAttribute.Value = @"C:\DeskTop";
                fileLocationTag.Attributes.Append(IdAttribute);
    Creation of your 4 child elements ...
    Code:
                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 ...
    Code:
                doc.Save(@"c:\somefile.txt");
    I think with this information you know enough to find how to load the files yourself .
    Attached Images Attached Images  
    Last edited by BramVandenbon; Aug 10th, 2007 at 09:59 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width