Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello ,
I'm new with C# Linq.
I have a code to combine 2 xmls and save it in a merged.xml and worked perfectly when both xml sources were in local folders ("C:\..\..."). Now I'm trying to load the same xml of one data source from a web url and I can't retrieve the xml successfully into g xElement. With xmlTextReader I get the xml from the web and I'm printing it in the console correctly formated but I don't know how to load this xml in my g xElement to still combining sucessfully with the first and local xml doc1.xml .

The main problem it's I don't know how to load/parse the xml data from the web into 'g' xmlElement.

C#
 String URLString = "http://www.XXX.com/XX/XX/getAllPackages";//Source of my xml
 XmlTextReader reader = new XmlTextReader(URLString);
                  
//Code is read and printing xml from the web url.
            while (reader.Read())
            {
            switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // Node is an element.
                        Console.Write("<" + reader.Name);

                        while (reader.MoveToNextAttribute()) // Read Atributtes
                        Console.Write(" " + reader.Name + "='" + reader.Value + "'");
                        Console.WriteLine(">");                        
                        break;
                    case XmlNodeType.Text: //Show text on each element.
                        Console.WriteLine (reader.Value);
                        break;
                    case XmlNodeType. EndElement: //Show the final element
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
       
            }

//Code to combine both xmls   
         

var query = (from bl in XElement.Load(@"C:\xx\doc1.xml").Elements("line_item_data").GroupBy(i1 =>
                            i1.Element("line_item").Value).Select(i1 => i1.First())

                         join g in XElement.Load(reader,LoadOptions.PreserveWhitespace).Elements("productVersionDetails").GroupBy(i1 =>
                            i1.Element("product").Value).Select(i1 => i1.First())
                     on (string)bl.Element("line_item").Value equals (string)g.Element("product").Value
              where ((string)g.Element("type")).Equals("MSD")                               

              select new XElement("product_item",

                   new XElement("version_id", (string)g.Element("version_id")),
                   new XElement("technology_process",(string)bl.Element("technology_process")),
                   new XElement("product", (string)g.Element("product")),
                   new XElement("version", (string)g.Element("version")),
                   new XElement("status", (string)g.Element("status")),
                   new XElement("owner_name", (string)bl.Element("owner_name")),
                   new XElement("description", (string)g.Element("description")),
                   new XElement("os", (string)g.Element("os"))

             )).ToList();

            //Save the new full List in the path data source File 
         var doc = new XElement("ArrayOfProductVersionDetails", query);
         doc.Save(@"C:\xxx\xx\XML_Merged.xml");           
         Console.WriteLine("XML merged and file saved.");
Posted
Updated 28-Feb-14 4:01am
v5

I'm afraid your XmlTextReader is already at the end of the stream; it got there when you were printing the stuff into console. It's logical that the second attempt to read it using XElement.Load without resetting the reader at the beginning fails. And, BTW, why don't you use XDocument class? In general, an XML document is not only its root element, it may contain some DOCTYPE, etc.
 
Share this answer
 
Comments
Kinna-10626331 3-Mar-14 4:02am    
Thanks VUnreal !! I follow your advice , you can check it in my answer.
VUnreal looks right. Try to load the xml with XmlDocument class which will load xml content properly if it has DTD otherwise try using XDocument class to load xml in your code.
 
Share this answer
 
Comments
Kinna-10626331 3-Mar-14 4:03am    
Thanks KmgKrishnan !! I follow your advice also , you can check it in my answer.
Solution:

Thanks! At the end I follow the advice of VUnreal and KmgKrishnan, I insert the xml data from the url in a xmlDocument and after I load this xml on a XElement in the join as I was doing before from local paths. It Works :) .



C#
          //Loading in  myXmlDocument the xml  from the url 
            string m_strFilePath = "http://www.XXX.com/XX/XX/getAllPackages";
            XmlDocument myXmlDocument = new XmlDocument();           
            //myXmlDocument.Save(Console.Out); 
            myXmlDocument.Load(m_strFilePath);



var query = (from bl in XElement.Load(@"C:\xx\doc1.xml").Elements("line_item_data").GroupBy(i1 =>
                            i1.Element("line_item").Value).Select(i1 => i1.First())

                         join g in XElement.Load(new XmlNodeReader(myXmlDocument)).Elements("productVersionDetails").GroupBy(i1 =>
                            i1.Element("product").Value).Select(i1 => i1.First())
                     on (string)bl.Element("line_item").Value equals (string)g.Element("product").Value
              where ((string)g.Element("type")).Equals("MSD")

              select new XElement("product_item",

                   new XElement("version_id", (string)g.Element("version_id")),
                   new XElement("technology_process",(string)bl.Element("technology_process")),
                   new XElement("product", (string)g.Element("product")),
                   new XElement("version", (string)g.Element("version")),
                   new XElement("status", (string)g.Element("status")),
                   new XElement("owner_name", (string)bl.Element("owner_name")),
                   new XElement("description", (string)g.Element("description")),
                   new XElement("os", (string)g.Element("os"))

             )).ToList();

            //Save the new full List in the path data source File
         var doc = new XElement("ArrayOfProductVersionDetails", query);
         doc.Save(@"C:\xxx\xx\XML_Merged.xml");
         Console.WriteLine("XML merged and file saved.");
 
Share this answer
 
Comments
KmgKrishnan 4-Mar-14 1:41am    
Good to know that it works fine for you :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900