XDocument解析但无法从字符串中搜索 [英] XDocument parses but isnt searchable from string
问题描述
我从一个看起来像这样的网站上得到一个字符串
I'm getting a string returned from a website that looks like this
<?xml version=\"1.0\" encoding=\"UTF-8\"?><searchResponse requestID=\"500\" status=\"success\"><pso><psoID ID=\"61F2C644-F93A-11DE-8015-73A11AB14291\" targetID=\"mezeoAccount\"><data><email>sholobfc@bluefire.com.au</email><quotaMeg>2048</quotaMeg><quotaUsed>1879736</quotaUsed><active>true</active><unlocked>true</unlocked><allowPublic>true</allowPublic><realm>mezeo</realm><bandwidthQuota>1000000000</bandwidthQuota><billingDay>1</billingDay></data></psoID></pso></searchResponse>"
然后我尝试从中创建一个XDocument,以便我可以通过元素进行枚举
I then try and create an XDocument from it so I can enumerate through the elements
XDocument doc = new XDocument();
doc = XDocument.Parse(respStr);
但是如果我每次查询元素或后代时都返回null.我不能走
but if I query the elements or descendants everytime it returns null. I can't go
string s = doc.Element("email").Value;
// or
doc.Descendants("data"); // returns null as well
XDocument.Parse不会返回错误,但是我似乎没有可搜索的xDocument.
XDocument.Parse doesn't return an error, but I don't seem to have a searchable xDocument.
有人能看到我所做的事情明显有问题吗?
Can anyone see anything obviously wrong with what I am doing?
推荐答案
在调用XDocument.Parse之前不需要创建新的XDocument.这不会造成任何问题,只是没有意义.
You don't need to create a new XDocument before calling XDocument.Parse. This won't cause any problems, it's just pointless.
但是,此行是错误的,因为电子邮件不是文档根目录的子代:
However, this line is wrong because email is not a child of the document root:
doc.Element("email").Value;
您的第二个示例看起来不错.这对我有用:
Your second example looks fine. This works for me:
string s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><searchResponse requestID=\"500\" status=\"success\"><pso><psoID ID=\"61F2C644-F93A-11DE-8015-73A11AB14291\" targetID=\"mezeoAccount\"><data><email>sholobfc@bluefire.com.au</email><quotaMeg>2048</quotaMeg><quotaUsed>1879736</quotaUsed><active>true</active><unlocked>true</unlocked><allowPublic>true</allowPublic><realm>mezeo</realm><bandwidthQuota>1000000000</bandwidthQuota><billingDay>1</billingDay></data></psoID></pso></searchResponse>";
XDocument doc = XDocument.Parse(s);
foreach (XElement e in doc.Descendants("data"))
Console.WriteLine(e);
结果:
<data>
<email>sholobfc@bluefire.com.au</email>
<quotaMeg>2048</quotaMeg>
<quotaUsed>1879736</quotaUsed>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<realm>mezeo</realm>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>
</data>
针对您的第二个第三个问题(请参阅此答案的注释),尝试以下操作:
In response to your second third question (see comments to this answer) try this:
using System;
using System.Xml.XPath;
using System.Xml.Linq;
class Program
{
public static void Main()
{
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><searchResponse requestID=\"500\" status=\"success\"><pso><psoID ID=\"61F2C644-F93A-11DE-8015-73A11AB14291\" targetID=\"mezeoAccount\"><data><email>sholobfc@bluefire.com.au</email><quotaMeg>2048</quotaMeg><quotaUsed>1879736</quotaUsed><active>true</active><unlocked>true</unlocked><allowPublic>true</allowPublic><realm>mezeo</realm><bandwidthQuota>1000000000</bandwidthQuota><billingDay>1</billingDay></data></psoID></pso></searchResponse>";
XDocument doc = XDocument.Parse(xml);
foreach (XElement e in doc.XPathSelectElements("/searchResponse/pso/psoID/data/*"))
Console.WriteLine(e);
}
}
输出:
<email>sholobfc@bluefire.com.au</email>
<quotaMeg>2048</quotaMeg>
<quotaUsed>1879736</quotaUsed>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<realm>mezeo</realm>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>
这篇关于XDocument解析但无法从字符串中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!