使用 Linq 解析 XML 时序列不包含元素
我有一个像这样的XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SampleResponse xmlns="http://tempuri.org/">
<SampleResult>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<tag1>tag1 text</tag1>
<tag2>tag2 text</tag2>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<tag1>tag1 text</tag1>
<tag2>tag2 text</tag2>
</Table>
</NewDataSet>
</diffgr:diffgram>
</SampleResult>
</SampleResponse>
</soap:Body>
</soap:Envelope>
这里我正在解析上面的XML,如下所示:
// My parser Class
class ParseClass
{
public string tag1 { get; set; }
public string tag2 { get; set; }
}
我的解析代码是:
string XMLresponse = e.response;
var XResult = XElement.Parse(XMLresponse);
var result = XResult.Descendants("Table").Select(t => new ParseClass
{
tag1 = t.Descendants("tag1").First().Value,
tag2 = t.Descendants("tag2").First().Value,
});
foreach (var res in result1)
{
string str=res.tag1;
str=res.tag2;
}
如果所有标签都来了,我就能够成功解析上面的XML。但有时我的响应 XML 缺少名为 tag2 的标签,当时我无法解析 XML 并给出类似 “序列不包含元素” 的异常。
这里我的要求是: 我尝试过 FirstOrDefault 方法来代替 First 方法,但没有用。 如果 XML 中缺少任何标记,则该对象的该变量应为 Null(即:如果缺少 tag2,则 res.tag2 应为 Null)。我怎样才能做到这一点?
I have a XML Like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SampleResponse xmlns="http://tempuri.org/">
<SampleResult>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<tag1>tag1 text</tag1>
<tag2>tag2 text</tag2>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<tag1>tag1 text</tag1>
<tag2>tag2 text</tag2>
</Table>
</NewDataSet>
</diffgr:diffgram>
</SampleResult>
</SampleResponse>
</soap:Body>
</soap:Envelope>
here i am parsing above XML Like Below:
// My parser Class
class ParseClass
{
public string tag1 { get; set; }
public string tag2 { get; set; }
}
My parsing Code is:
string XMLresponse = e.response;
var XResult = XElement.Parse(XMLresponse);
var result = XResult.Descendants("Table").Select(t => new ParseClass
{
tag1 = t.Descendants("tag1").First().Value,
tag2 = t.Descendants("tag2").First().Value,
});
foreach (var res in result1)
{
string str=res.tag1;
str=res.tag2;
}
I am able parse above XML successfully if all the tags are coming. But some times my response XML is missing tag called tag2, at that time i am not able to parse the XML and giving exception like "sequence contains no elements".
Here my requirement is:
I have tried FirstOrDefault method in place of First method but no use.
if any tag is missing in XML then that variable should be Null for that Object(i.e: if tag2 is missing then res.tag2 should be null). How can i achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用
use
(对于 tag2 也是如此。),
如果值不存在,则
FirstOrDefault()
将返回null
, 并且XElement
当要求转换 null 引用时,字符串转换将返回 null,否则返回文本内容。Instead of using
use
(And likewise for tag2.)
FirstOrDefault()
will returnnull
if the value isn't present, and theXElement
to string conversion will return null when asked to convert a null reference, or the text content otherwise.