使用 LINQ 问题解析 XML
Parsing XML With LINQ Issue
样本XML:
<Response xmlns="http://tempuri.org/">
<Result xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:string>18c03787-9222-4c9b-8f39-44c2b39c788e</a:string>
<a:string>774d38d2-a350-4711-8674-b69404283448</a:string>
</Result>
</Response>
当我尝试解析这段代码时,我返回的是空值,即:
XNamespace temp = "http://tempuri.org/";
XDocument xdoc = XDocument.Parse(xml);
以下所有情况return null:
xdoc.Descendants(temp + "Result")
xdoc.Descendants();
xdoc.Element(temp + "Result");
我误会了什么?
****** 编辑 **********
抱歉浪费大家的时间。
看来我使用的是 http://www.tempuri.org instead of http://tempuri.org,但在我的问题中错误地列出了正确的那个。
以下是如何通过几个明确的步骤提取值的方法:
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace WaitForIt
{
class Program
{
static void Main(string[] args)
{
string thexml = @"<Response xmlns=""http://tempuri.org/""><Result xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:string>18c03787-9222-4c9b-8f39-44c2b39c788e</a:string><a:string>774d38d2-a350-4711-8674-b69404283448</a:string></Result></Response>";
XDocument doc = XDocument.Parse(thexml);
XNamespace ns = "http://tempuri.org/";
var result = doc.Descendants(ns + "Result");
var resultStrings = result.Elements();
foreach (var el in resultStrings)
{
Debug.WriteLine(el.Value);
}
// output:
// 18c03787-9222-4c9b-8f39-44c2b39c788e
// 774d38d2-a350-4711-8674-b69404283448
}
}
}
样本XML:
<Response xmlns="http://tempuri.org/">
<Result xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:string>18c03787-9222-4c9b-8f39-44c2b39c788e</a:string>
<a:string>774d38d2-a350-4711-8674-b69404283448</a:string>
</Result>
</Response>
当我尝试解析这段代码时,我返回的是空值,即:
XNamespace temp = "http://tempuri.org/";
XDocument xdoc = XDocument.Parse(xml);
以下所有情况return null:
xdoc.Descendants(temp + "Result")
xdoc.Descendants();
xdoc.Element(temp + "Result");
我误会了什么?
****** 编辑 **********
抱歉浪费大家的时间。 看来我使用的是 http://www.tempuri.org instead of http://tempuri.org,但在我的问题中错误地列出了正确的那个。
以下是如何通过几个明确的步骤提取值的方法:
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace WaitForIt
{
class Program
{
static void Main(string[] args)
{
string thexml = @"<Response xmlns=""http://tempuri.org/""><Result xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:string>18c03787-9222-4c9b-8f39-44c2b39c788e</a:string><a:string>774d38d2-a350-4711-8674-b69404283448</a:string></Result></Response>";
XDocument doc = XDocument.Parse(thexml);
XNamespace ns = "http://tempuri.org/";
var result = doc.Descendants(ns + "Result");
var resultStrings = result.Elements();
foreach (var el in resultStrings)
{
Debug.WriteLine(el.Value);
}
// output:
// 18c03787-9222-4c9b-8f39-44c2b39c788e
// 774d38d2-a350-4711-8674-b69404283448
}
}
}