CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97

    Writing and Appending XML with C#

    Does anyone have any simple examples of writing to an XML document, and then also opening and appending to it after "drilling" down a couple elements? Everything I have found displays information on how to create and write an XML document, which I have down. I can't, however, find anything on reopening the document and then adding more elements and/or data to it.

    Thanks

  2. #2
    Join Date
    Jan 2003
    Posts
    13
    Here you go:

    Code:
    using System;
    using System.Xml;
    
    class XmlIO
    {
        static void Main()
        {
            //sample.xml is a simple xml document that exists
            //in the root directory of the executable
            XmlDocument source = new XmlDocument();
            source.Load("sample.xml");
    
            Console.WriteLine(source.DocumentElement.SelectSingleNode("make").InnerText);
    
            XmlElement node = source.CreateElement("model");
            node.InnerText = "tt";
            source.DocumentElement.AppendChild(node);
    
            Console.WriteLine(source.DocumentElement.SelectSingleNode("model").InnerText);
            source.Load("sample.xml");
        }
    }

  3. #3
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    Thanks!

  4. #4
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    I think I may be missing something here. I am able to load the XML document, and do what looks to be a drill-down into the root element, but it isn't working. For instance, with the sample document below:

    ==============================================
    <?xml version="1.0" ?>
    <!-- Saved Calculations for further reference -->
    <SavedCalculations>
    <Item>
    <ItemName>Sample Item Name</ItemName>
    <ItemValue>10.43</ItemValue>
    </Item>
    </SavedCalculations>
    ==============================================

    I just want to drill down to the 'SavedCalculations' element, and then add a new <Item> with its corresponding <ItemName> and <ItemValue>, so it would now look like this:

    ==============================================
    <?xml version="1.0" ?>
    <!-- Saved Calculations for further reference -->
    <SavedCalculations>
    <Item>
    <ItemName>Sample Item Name</ItemName>
    <ItemValue>10.43</ItemValue>
    </Item>
    <Item>
    <ItemName>Sample Item Name2</ItemName>
    <ItemValue>46.03</ItemValue>
    </Item>
    </SavedCalculations>
    ==============================================

    Would the example you gave perform this if I modified it like this:

    ==============================================
    XmlDocument source = new XmlDocument();
    source.Load("sample.xml");

    Console.WriteLine(source.DocumentElement.SelectSingleNode("SavedCalculations").InnerText);

    XmlElement node = source.CreateElement("Item");

    XmlElement node = source.CreateElement("ItemName");
    node.InnerText = "Saved Item Name2";
    source.DocumentElement.AppendChild(node);

    XmlElement node = source.CreateElement("ItemValue");
    node.InnerText = "46.03;
    source.DocumentElement.AppendChild(node);

    Console.WriteLine(source.DocumentElement.SelectSingleNode("model").InnerText);
    source.Load("sample.xml");
    ==============================================


    Thanks!

  5. #5
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    Sorry...just double-checked, and the code actually looks like this:

    XmlDocument source = new XmlDocument();
    source.Load(m_SavedCalculationDocument);

    XmlElement node = source.CreateElement("Item");

    XmlElement node2 = source.CreateElement("ItemName");
    node.InnerText = "Sample Item Name2";
    source.DocumentElement.AppendChild(node2);

    XmlElement node3 = source.CreateElement("ItemValue");
    node.InnerText = "46.03";
    source.DocumentElement.AppendChild(node3);

    source.Load(m_SavedCalculationDocument);

  6. #6
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    Never mind...just got it.

  7. #7
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    could you tell us how did you got that. was busy such a long 2-3 days.



    apreciate if you show us your solution.

    Paresh
    - Software Architect

  8. #8
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    I pretty much used this online application's source and modified the AddContact() method to accomodate my specific needs:

    http://www.mastercsharp.com/article....=69&&TopicID=9

    The entire XML.NET area of this site looks to be down right now though, so you may want to check back later.

    For those who are looking to utilize XML functinoality via C#, this is an excellent example with comments. I learned just about as much with it as I did in my C# book's explanation and examples with XML.

  9. #9
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    oh ! I din't know that's an XML site.
    i will check back later.

    glad to hear back from you very soon

    Paresh
    - Software Architect

  10. #10
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    Actually, it looks like the entire site is down. I just remembered that I pulled the source and readme for the app earlier today though, so I have attached it for those who are interested in taking a look.
    Attached Files Attached Files

  11. #11
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    thanks for your attachment. and hope you keep posting good code.


    Paresh
    - Software Architect

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

Featured