Improve company productivity with a Business Account.Sign Up

x
?
Solved

How do I add an entire XMLdocument to a file

Posted on 2010-09-14
7
Medium Priority
?
506 Views
Last Modified: 2012-08-14
I am building up a results file using XMLDiffPatch.

Each test creates a XML file. I want to combine all the outputs to a single file I have built up using XElement.

The trouble is I can not see how to add an entire document to my XElement node.
If I try using OuterXML etc it escapes all the < and > etc.

Please see my attached code
XmlDocument xResult = new XmlDocument();
                xResult.  Load("result.xml");
                XmlElement root = xResult.DocumentElement;

                XElement tempNode = XMLUtilities.AddElement(requestNode, "result", "");
                tempNode.add(root);

//  XMLUtilities.AddElement(requestNode, "result", root.OuterXml, "same", "false");

Open in new window

0
Comment
Question by:jetbet
  • 4
  • 3
7 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 33679029
Where is requestNode defined?
I think you need to create an xml document and add the result child nodes. Some pseudo code:
XmlDocument xTotalResult = new XmlDocument();
XmlElement rootTotal = xTotalResult.DocumentElement
for each file in Files
  XmlDocument xResult = new XmlDocument();
  xResult. Load("result.xml");
  XmlElement root = xResult.DocumentElement;
  XElement tempNode = xTotalResult.CreateElement("Result" + file.FileName);
  //Add root.ChildNodes to tempNode
  rootTotal.AppendChild(tempNode);

Let me know if need more info
0
 
LVL 1

Author Comment

by:jetbet
ID: 33679066
requestNode is an XElement. The way I am doing it is to append the results to the DOM each time around a loop (new request) and then save it at the end

It appears from your pseudocode that I need to iterate through the entire result tree creating nodes as I go and copying the details over. Is this correct?

I have attached more code to show how the thing holds togeather.





private void BT_Compare_Click(object sender, EventArgs e)
        {
            XElement xml_doc = new XElement("document", "");
            CompareMeetings(xml_doc, "CGI", "Test");

            SaveToFile(xml_doc, "comparisons.xml");
        }

private void CompareResponses(XElement requestNode, String request, XmlDocument firstXML, XmlDocument secondXML)
        {

            String firstResponse = Requests.DoRequestResponse(firststream, request);
            String secondResponse = Requests.DoRequestResponse(secondstream, request);

            firstXML.Load(new System.IO.StringReader(firstResponse));
            secondXML.Load(new System.IO.StringReader(secondResponse));

            XmlWriter output = XmlWriter.Create("result.xml");

            if (DiffTool.Compare(firstXML, secondXML, output))
            {
                XMLUtilities.AddElement(requestNode, "result", "", "same", "true");
            }
            else
            {
                output.Close();
                XmlDocument xResult = new XmlDocument();
                xResult.  Load("result.xml");
                XmlElement root = xResult.DocumentElement;

            //    XElement tempNode = XMLUtilities.AddElement(requestNode, "result", "");
            
               XMLUtilities.AddElement(requestNode, "result", root.OuterXml, "same", "false");
            }
            output.Close();
        }

private void CompareMeetings(XElement xml_doc,  String system, String systemID)
        {
            XElement requestNode = XMLUtilities.AddElement(xml_doc, "request", "", "type", "Get_Meetings");

            try
            {
                XmlDocument firstXML = new XmlDocument();
                XmlDocument secondXML = new XmlDocument();
                String request = Requests.GetGenericString(system, systemID, "Get_Meetings", "All", "", "");
                
                CompareResponses(requestNode, request, firstXML, secondXML);
               
                XmlNodeList meetings = firstXML.SelectNodes("Response/Meetings/Meeting");
                foreach (XmlNode meet in meetings)
                {
                    String number = meet.SelectSingleNode("Meeting_Number").InnerText;
                    CompareEvents(xml_doc, system, systemID, number);
                }
            }
            catch (Exception e1)
            {
                XMLUtilities.AddElement(requestNode, "error", e1.Message);
            }
        }

Open in new window

0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 33679093
Yes it is correct. This will avoid entering duplicate root result nodes.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 1

Author Comment

by:jetbet
ID: 33679138
There is no chance of creating duplicate result nodes as I put the result tree inside request tags which in turn are inseide a <document> root tag.

I will try and manipulate the text via the  root.OuterXml call as this is almost correct. The only issue is that it is treated as text and so the tags are escaped.

Here is the (almost) correct output. Although this looks correct the underlying code has < etc around any of the "xd:" elements.
<?xml version="1.0" ?> 
- <document>
- <request type="Get_Meetings">
  <result same="false"><xd:xmldiff version="1.0" srcDocHash="3565019143749008783" options="None" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff"><xd:remove match="1" /><xd:node match="2"><xd:remove match="3" /><xd:node match="4"><xd:change match="1">8703</xd:change></xd:node><xd:node match="5"><xd:change match="1">Jetbet Down</xd:change></xd:node><xd:add><Customer_Text>System is not currently available</Customer_Text></xd:add><xd:remove match="7-19" /></xd:node></xd:xmldiff></result> 
  </request>
- <request type="Get_Events" meeting="2">
  <result same="false"><xd:xmldiff version="1.0" srcDocHash="9942230457533506927" options="None" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff"><xd:remove match="1" /><xd:node match="2"><xd:remove match="3" /><xd:node match="4"><xd:change match="1">8703</xd:change></xd:node><xd:node match="5"><xd:change match="1">Jetbet Down</xd:change></xd:node><xd:add><Customer_Text>System is not currently available</Customer_Text></xd:add><xd:remove match="7-14" /></xd:node></xd:xmldiff></result> 
  </request>
- <request type="Get_Events" meeting="3">
  <result same="false"><xd:xmldiff version="1.0" srcDocHash="16552962767753773473" options="None" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff"><xd:remove match="1" /><xd:node match="2"><xd:remove match="3" /><xd:node match="4"><xd:change match="1">8703</xd:change></xd:node><xd:node match="5"><xd:change match="1">Jetbet Down</xd:change></xd:node><xd:add><Customer_Text>System is not currently available</Customer_Text></xd:add><xd:remove match="7-14" /></xd:node></xd:xmldiff></result> 
  </request>

Open in new window

0
 
LVL 1

Author Comment

by:jetbet
ID: 33679147
Sorry you can not see the & l t ;stuff as the browser hides it
0
 
LVL 1

Author Comment

by:jetbet
ID: 33687120
I have taken your advice and created new XElements to insert. I have added the code used to help others.

Thanks for your advice.


XElement parent = XMLUtilities.AddElement(requestNode, "result", "", "same", "false");

                XmlDocument xResult = new XmlDocument();
                xResult.Load("result.xml");
                XmlNode root = xResult.DocumentElement;

                DeepCopy(root, parent);


private void DeepCopy(XmlNode source, XElement parent)
        {
            String name = source.LocalName;
            String value = "";
            XmlNodeList children = source.ChildNodes;
            foreach (XmlNode child in children)
            {
                if (child.NodeType == XmlNodeType.Text)
                {
                    value = child.Value;
                    break;
                }
            }

            XElement newParent = XMLUtilities.AddElement(parent, name, value);
            XmlAttributeCollection attributes = source.Attributes;
            foreach (XmlAttribute attribute in attributes)
            {
                String attname = attribute.LocalName;
                String attvalue = attribute.Value;
                XAttribute lAttribute = new XAttribute(attname, attvalue);
                newParent.Add(lAttribute);
            }
            
            foreach (XmlNode child in children)
            {
                if (child.NodeType == XmlNodeType.Element)
                {
                    DeepCopy(child, newParent);
                }
            }

        }

Open in new window

0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 33687138
Good on you, happy coding
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Watch the working video to know how to import Outlook PST/OST files to Amazon WorkMail. Kernel released this tool which is very easy to use and migrate single or multiple PST and OST files to Amazon WorkMail. To know more about Kernel Import PST to …

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question