CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2011
    Posts
    6

    Question How do modify a large XML file to become smaller?

    I get a larger XML file from an outside source. I would like to read specific elements and write them to a new XML file for my intended user. My user doesn't need the other elements.

    I tried this but I don't think I'm navigating the hierarchy correctly.

    Code:
                XElement xml = XElement.Load("test.xml");
                XElement xml2 = new XElement("applications",
                                        new XElement("application",
                                           new XElement("uid", xml.Element("uid").Value)));
       
                xml2.Save(@"C:\newfile.xml");
    Let say the XML looks like this:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <applications>
    <applicant>
    <uid>321321321</uid>
    <address>321321321</address>
    <phone>321321321</phone>
    <email>321321321</email>
    </applicant>
    </applications>
    And I just want a file like this in the end:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <applications>
    <applicant>
    <uid>321321321</uid>
    </applicant>
    </applications>
    I also know I need a loop in there somewhere.

    Any help is appreciated.

    Thanks.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How do modify a large XML file to become smaller?

    This worked for me (I have no doubt that it can be refactored to produce smarter code).... cg.xml is the original file cg2.xml is the cut-down file:
    Code:
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace CG
    {
        class Program
        {
            static void Main(string[] args)
            {
                XDocument doc;
    
                using (FileStream fs = new FileStream(@"C:\cg.xml", FileMode.Open))
                {
                    doc = XDocument.Load(fs);
    
                    XElement applications = doc.Element("applications");
    
                    foreach (XElement applicant in applications.Elements("applicant"))
                    {
                        XElement badElement = applicant.Element("address");
                        badElement.Remove();
    
                        badElement = applicant.Element("phone");
                        badElement.Remove();
    
                        badElement = applicant.Element("email");
                        badElement.Remove();
                    }
                }
    
                using (FileStream fs = new FileStream(@"C:\cg2.xml", FileMode.Create))
                {
                    XmlWriter writer = XmlWriter.Create(fs);
    
                    doc.WriteTo(writer);
    
                    writer.Close();
                }
            }
        }
    }
    Last edited by rliq; September 28th, 2011 at 11:15 PM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Sep 2011
    Posts
    6

    Re: How do modify a large XML file to become smaller?

    Thanks. I'll try this if nothing else comes up. I was hoping to select instead of delete elements. The original files has 500 columns to sort.

  4. #4
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: How do modify a large XML file to become smaller?

    This method should provide you with what you're looking for... you will need to tweak it to fit your exactl XML layout/needs... but the basic idea is here.

    Code:
                XmlDocument sourceDoc = new XmlDocument();
                XmlDocument targetDoc = new XmlDocument();
    
                sourceDoc.Load("ORIG.xml");
    
                XmlNode newAppsNode = targetDoc.AppendChild(targetDoc.CreateElement("applications"));
    
                foreach (XmlNode node in sourceDoc.DocumentElement.SelectNodes("applications/applicant"))
                {
                    XmlNode newAppNode = targetDoc.CreateElement("applicant");
                    newAppsNode.AppendChild(newAppNode.AppendChild(targetDoc.ImportNode(node.SelectSingleNode("uid"), true)));
                }
    
                targetDoc.Save("NEW.xml");

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How do modify a large XML file to become smaller?

    No worries. Pretty sure that it can be done in a LINQ query (or two).
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  6. #6
    Join Date
    Sep 2011
    Posts
    6

    Re: How do modify a large XML file to become smaller?

    Quote Originally Posted by fcronin View Post
    This method should provide you with what you're looking for... you will need to tweak it to fit your exactl XML layout/needs... but the basic idea is here.
    I made one change to your code in the foreach section. It looped as expected after the edit. My problem is that I can't get it to write the node called 'applicant'.

    Code:
                           XmlNode newAppsNode = targetDoc.AppendChild(targetDoc.CreateElement("applications"));
                
    
                /*foreach (XmlNode node in sourceDoc.DocumentElement.SelectNodes("applications/applicant"))*/
                foreach (XmlNode node in sourceDoc.DocumentElement.SelectNodes("applicant"))
                {
                    XmlNode newAppNode = targetDoc.CreateElement("applicant");
                    newAppsNode.AppendChild(
                        newAppNode.AppendChild(
                            targetDoc.ImportNode(node.SelectSingleNode("uid"), true)));
                }
    
                targetDoc.Save("TestSupplemental.xml");
    All I get is this:

    Code:
    <applications>
      <uid>111111111</uid>
      <uid>222222222</uid>
      <uid>333333333</uid>
    </applications>
    Thanks for your help.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How do modify a large XML file to become smaller?

    Why don't you break this down into steps?

    Instead of trying to appead the application node and it's child nodes, just try to get the application node to show up.

    Code:
    XmlNode newAppsNode = targetDoc.AppendChild(targetDoc.CreateElement("applications"));
                
    foreach (XmlNode node in sourceDoc.DocumentElement.SelectNodes("applicant"))
    {
      XmlNode newAppNode = targetDoc.CreateElement("applicant");
      newAppsNode.AppendChild( newAppNode );
    }
    When you get this working, go back and add the uid child nodes.

  8. #8
    Join Date
    Sep 2011
    Posts
    6

    Re: How do modify a large XML file to become smaller?

    Quote Originally Posted by Arjay View Post
    Why don't you break this down into steps?
    Got it. Thanks. Everything works now. I'll be able to specify elements in the source file and create unique files for my department.

    Code:
                XmlDocument sourceDoc = new XmlDocument();
                XmlDocument targetDoc = new XmlDocument();
    
                sourceDoc.Load("source.xml");
    
                XmlNode newAppsNode = targetDoc.AppendChild(targetDoc.CreateElement("applications"));
    
                foreach (XmlNode node in sourceDoc.DocumentElement.SelectNodes("applicant"))
                {
                    XmlNode newAppNode = targetDoc.CreateElement("applicant");
                    
                    newAppsNode.AppendChild(newAppNode);
                        
                        /* Repeat below for different columns */
                        newAppNode.AppendChild(
                            targetDoc.ImportNode(node.SelectSingleNode("name"), true));
                }
    
                targetDoc.Save("DeptSupplemental.xml");
            }

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How do modify a large XML file to become smaller?

    It's kind of important to understand what went wrong before.

    Code:
    newAppsNode.AppendChild(
                        newAppNode.AppendChild(
                            targetDoc.ImportNode(node.SelectSingleNode("uid"), true)));
    Before you were searching for a uid node in the target doc. I'm guessing that that node didn't exist, so you attempted to append a null node to the newAppNode. That failed causing the newAppsNode.AppendChild to also fail (or the code to throw an exception that you didn't catch).

    The bottom line is while you are learning, resist the urge to peform multiple operations in one call. AppendChild within another AppendChild call may mask problems. At first, it's better to break these out on different lines so you can step through each line in a debugger.

    For newbies, many questions stem from the fact that they don't step through code in a debugger. Once they learn to do this, they can usually figure out problems on their own.

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