本文介绍了将URL从URL Web加载/解析为XElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 我是C#Linq的新手。 我有一个代码可以合并2 xmls并将其保存在 merged.xml 并且当两个xml源都在本地文件夹(C:\..\ ...)中时工作得很好。现在我正在尝试从 web url 加载一个数据源的相同xml,我无法将xml成功检索到 g xElement。使用xmlTextReader,我从网上获取xml,然后我在控制台中正确格式化它,但我不知道如何在我的 g xElement中加载这个xml仍然成功地与第一个和本地xml doc1.xml。 主要问题是我不知道如何将网络上的xml数据加载/解析为'g' xmlElement。 字符串 URLString = http://www.XXX.com/XX/XX/getAllPackages; // 我的xml的来源 XmlTextReader reader = new XmlTextReader (URLString); // 读取代码并从网址打印xml。 while (reader.Read()) { switch ( reader.NodeType) { case XmlNodeType.Element: // 节点是一个元素。 Console.Write( < + reader.Name); while (reader.MoveToNextAttribute()) // 读取Atributtes Console.Write( + reader.Name + =' + reader.Value + '); Console.WriteLine( >); break ; case XmlNodeType.Text: // 显示每个元素上的文字。 Console.WriteLine(reader.Value); break ; case XmlNodeType。 EndElement: // 显示最终元素 Console.Write( < / + reader.Name); Console.WriteLine( >); break ; } } // 代码到结合两个xmls var query =( from bl in XElement.Load( @ C: \xx\doc1.xml)。元素( line_item_data)。 GroupBy(i1 = > i1.Element( line_item)。值).Select(i1 = > i1.First()) XElement.Load(reader,LoadOptions.PreserveWhitespace).Elements( join g > productVersionDetails)。GroupBy(i1 = > i1.Element( product)。Value).Select(i1 = > i1.First() ) on( string )bl.Element( line_item)。值等于( string )g.Element( product)。值 where (( string )g.Element( type))。Equals( MSD) 选择 new XElemen t( 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,(字符串)g.Element( version)), new XElement( status, ( string )g。元素( status)), new XElement( owner_name,( string )bl.Element( owner_name)), new XElement( description,( string )g。元素( description)), new XElement( os,( string )g.Element( os)) ))ToList(); // 将新的完整列表保存在路径数据源文件 $中b $ b var doc = new XElement( ArrayOfProductVersionDetails,query); doc.Save( @ C:\ xxx \ xxd \ XML_Merged.xml); Console.WriteLine( XML已合并并保存文件。); 解决方案 我担心您的XmlTextReader已经在流的末尾;当你把东西打印到控制台时它就到了那里。合乎逻辑的是,第二次使用XElement.Load读取它而不重置读取器的尝试失败了。而且,BTW,为什么不使用XDocument类?通常,XML文档不仅是它的根元素,它可能包含一些DOCTYPE等。 VUnreal看起来正确。尝试使用XmlDocument类加载xml,如果它有DTD则会正确加载xml内容,否则尝试使用XDocument类在代码中加载xml。 解决方案: 谢谢!最后,我遵循VUnreal和KmgKrishnan的建议,我在xmlDocument中插入来自url的xml数据,然后我在连接的XElement上加载这个xml,就像我之前从本地路径做的那样。它工作:)。 // 在myXmlDocument中加载来自网址的xml 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:\ xt \ _doc1.xml)。元素( line_item_data)。GroupBy(i1 = > i1.Element( line_item)。值)。选择(i1 = > i1.First()) join g in XElement.Load( new XmlNodeReader(myXmlDocument))。元素( productVersionDetails)。GroupBy(i1 = > i1.Element( product)。值)。选择(i1 = > i1.First()) on( string )bl.Element( line_item)。值等于( string )g.Element( product)。价值 where (( string )g.Element( type))。Equals( MSD) 选择 新 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。元素( version )), new XElement( status,( string )g。元素( status)), new XElement( owner_name,( string )bl.Element( owner_name)), new XElement( description,( string )g。元素( description)), new XElement( os,( string )g.Element( os)) ))。ToList(); // 将新的完整列表保存在路径数据源文件 $中b $ b var doc = new XElement( ArrayOfProductVersionDetails,query); doc.Save( @ C:\ xxx \ xxd \ XML_Merged.xml); Console.WriteLine( XML已合并并保存文件。); 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. 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 xmlsvar 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."); 解决方案 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.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.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 :) . //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."); 这篇关于将URL从URL Web加载/解析为XElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 14:01