使用 Linq 解析 XML 时序列不包含元素

发布于 2024-12-27 09:32:56 字数 2263 浏览 2 评论 0原文

我有一个像这样的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舟遥客 2025-01-03 09:32:56

而不是使用

tag1 = t.Descendants("tag1").First().Value

use

tag1 = (string) t.Descendants("tag1").FirstOrDefault()

(对于 tag2 也是如此。),

如果值不存在,则 FirstOrDefault() 将返回 null, 并且 XElement当要求转换 null 引用时,字符串转换将返回 null,否则返回文本内容。

Instead of using

tag1 = t.Descendants("tag1").First().Value

use

tag1 = (string) t.Descendants("tag1").FirstOrDefault()

(And likewise for tag2.)

FirstOrDefault() will return null if the value isn't present, and the XElement to string conversion will return null when asked to convert a null reference, or the text content otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文