LINQ to XML - 将嵌套命名空间内的值提取到对象中

发布于 2024-11-05 07:38:10 字数 2937 浏览 0 评论 0原文

我想使用 LINQ to XML 从具有嵌套命名空间的 XML 文档中提取值。

我的问题已由另一位 SO 用户部分回答: LINQ to XML w/嵌套命名空间

我仍然无法弄清楚如何将值选择到对象的属性中。

以下是解决此问题的资源:

示例 XML 文件链接

上面链接的内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Envelope xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns3="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4">
<ns1:Header>
    <ns3:TransactionID ns1:mustUnderstand="1">XXXX-XXXX-XXXX-XXXX</ns3:TransactionID>
</ns1:Header>
<ns1:Body>
<ns3:DeliverReq>
    <ns3:MM7Version>6.8.0</ns3:MM7Version>
    <ns3:LinkedID>LINKED-ID-IS-HERE</ns3:LinkedID>
    <ns3:Sender>
        <ns3:Number>3025551212</ns3:Number>
    </ns3:Sender>
    <ns3:Recipients>
        <ns3:To>
            <ns3:Number displayOnly="false">11111</ns3:Number>
        </ns3:To>
    </ns3:Recipients>
    <ns3:TimeStamp>2011-04-25T10:28:40.000Z</ns3:TimeStamp>
    <ns3:UACapabilities UAProf="motok1c"/>
    <ns3:Content allowAdaptations="true" href="cid:default.cid"/>
</ns3:DeliverReq>
</ns1:Body>
</ns1:Envelope>

对象从此 XML 中填充:

public class TestClass
{
    public string TransactionId { get; set; }
    public string MessageType { get; set; }
    public string Mm7Version { get; set; }
    public string VaspId { get; set; }
    public string VasId { get; set; }
    public string Sender { get; set; }
    public string Recipients { get; set; }
    public string LinkedId { get; set; }
    public string TimeStamp { get; set; }
    public string Priority { get; set; }
    public string Subject { get; set; }
    public string Content { get; set; }
    public string UaCapabilities { get; set; }

    // not all properties appear in every XML message received
}

SO 示例帮助我理解了添加命名空间 (XNamespace) 的必要性,但在尝试使用 LINQ 语句填充对象时,我遇到了不足。

我想我需要做一些类似的事情:

// (don't know the "from" in this case - decendants? 
// which XML key / node?)
select new TestClass
{
    TransactionId = [LINQ to select the "TransactionID" value that 
        appears under the Header key]
    Mm7Version = [LINQ to select the "MM7Version" value, under the 
        DeliverReq key]     
    .....
}

当您拥有上面发布的 XML 时,能够将值选择到对象的属性中的 LINQ 的形状是什么? 我关心 XML 的两个部分中的数据:标头中的 TransactionID 值和 DeliverReq 下的值。奇怪的是它们分散开来,并且比简单地从 DeliverReq 键中严格选择值更令人困惑。如果我看到 FROM 部分,以及 2 到 3 个属性,其中填充了 XML 中的值,那么我就能够从那里获取它。

如果您需要任何说明,请告诉我。我的主要问题是 LINQ 的“FROM”部分以及如何处理 TransactionId 位于 DeliverReq 标记之外的事实。我认为我可以处理其他情况,例如嵌套发件人和收件人标签下的值。

I'd like to use LINQ to XML to extract values from an XML doc that has nested namespaces.

My question had partially been answered by another SO user: LINQ to XML w/ nested namespaces

I am still having trouble trying to figure out how to select the values into an object's properties.

Here are the resources for this problem:

Sample XML File Link

Contents of above link:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Envelope xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns3="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4">
<ns1:Header>
    <ns3:TransactionID ns1:mustUnderstand="1">XXXX-XXXX-XXXX-XXXX</ns3:TransactionID>
</ns1:Header>
<ns1:Body>
<ns3:DeliverReq>
    <ns3:MM7Version>6.8.0</ns3:MM7Version>
    <ns3:LinkedID>LINKED-ID-IS-HERE</ns3:LinkedID>
    <ns3:Sender>
        <ns3:Number>3025551212</ns3:Number>
    </ns3:Sender>
    <ns3:Recipients>
        <ns3:To>
            <ns3:Number displayOnly="false">11111</ns3:Number>
        </ns3:To>
    </ns3:Recipients>
    <ns3:TimeStamp>2011-04-25T10:28:40.000Z</ns3:TimeStamp>
    <ns3:UACapabilities UAProf="motok1c"/>
    <ns3:Content allowAdaptations="true" href="cid:default.cid"/>
</ns3:DeliverReq>
</ns1:Body>
</ns1:Envelope>

Object to be filled from this XML:

public class TestClass
{
    public string TransactionId { get; set; }
    public string MessageType { get; set; }
    public string Mm7Version { get; set; }
    public string VaspId { get; set; }
    public string VasId { get; set; }
    public string Sender { get; set; }
    public string Recipients { get; set; }
    public string LinkedId { get; set; }
    public string TimeStamp { get; set; }
    public string Priority { get; set; }
    public string Subject { get; set; }
    public string Content { get; set; }
    public string UaCapabilities { get; set; }

    // not all properties appear in every XML message received
}

The SO example helped me understand the need to add a namespace (XNamespace), but I am falling short when trying to fill the object using a LINQ statement.

I think I need to do something like:

// (don't know the "from" in this case - decendants? 
// which XML key / node?)
select new TestClass
{
    TransactionId = [LINQ to select the "TransactionID" value that 
        appears under the Header key]
    Mm7Version = [LINQ to select the "MM7Version" value, under the 
        DeliverReq key]     
    .....
}

What is the shape of the LINQ to be able to select values into properties of an object when you have XML like posted above?
I care about the data in 2 portions of the XML: The TransactionID value in the header, and the values that are under the DeliverReq. It is odd to be that they are spread out and it is more confusing than simply selecting the values strictly from the DeliverReq key. If I saw the FROM portion, and 2 to 3 of the properties filled with values from the XML then I would be able to pick it up from there.

Please let me know if you need any clarifications. My main problems are with the "FROM" portion of the LINQ and how to deal with the fact that TransactionId is outside of the DeliverReq tag. I think I can handle the other cases, such as the nesting on the values under the Sender and Recipients tags.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

糖果控 2024-11-12 07:38:10

由于 XML 中只有一个 TestClass 实例的属性,并且这些值遍布各处,因此手动选择它们然后创建实例是有意义的:

XDocument doc = XDocument.Load(@"test.xml");
XNamespace ns3 = "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4";

string transactionId = doc.Descendants(ns3 + "TransactionID").Single().Value;
string mm7Version = doc.Descendants(ns3 + "MM7Version").Single().Value;
//...select the other elements

TestClass testClass = new TestClass() { TransactionId = transactionId, 
                                        Mm7Version = mm7Version};

Since there is only properties for one of your TestClass instances in your XML, and the values for those are all over the place it makes sense to select them manually and then create the instance:

XDocument doc = XDocument.Load(@"test.xml");
XNamespace ns3 = "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4";

string transactionId = doc.Descendants(ns3 + "TransactionID").Single().Value;
string mm7Version = doc.Descendants(ns3 + "MM7Version").Single().Value;
//...select the other elements

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