使用 XDocument 搜索和提取数据

发布于 2024-10-03 10:24:28 字数 1167 浏览 3 评论 0原文

我从 sql 返回一个包含多个地址的 xml 字符串,下面是返回内容的示例:

<Addresses>
  <Address>
    <LetterQueueOID>2</LetterQueueOID>
    <Address1>115 MORNINGVIEW TRL</Address1>
    <Address2>SCARBOROUGH, </Address2>
    <Address3>M1B5L2</Address3>
    <City>SCARBOROUGH</City>
    <PostalCode>M1B5L2</PostalCode>
  </Address>
  <Address>
    <LetterQueueOID>1</LetterQueueOID>
    <Address1>GD PO BOX 685</Address1>
    <Address2>THORNBURY, ON</Address2>
    <Address3>N0H2P0</Address3>
    <City>THORNBURY</City>
    <ProvinceOrState>ON</ProvinceOrState>
    <CountryCode>Ca</CountryCode>
    <PostalCode>N0H2P0</PostalCode>
  </Address>
</Addresses>

我想使用 LINQ 来查询此 xml 字符串以获取特定的 LetterQueueOID,即 选择地址(作为字符串),其中 LetterQueueOID = 2。

我所知道的是我可以使用 XDocument,但我无法确切地弄清楚如何获得我想要的内容:

XDocument addresses = XDocument.Parse((string)returnScalar);
IEnumerable<XElement> items = addresses.Root.Elements("Address").ToList();

I am returning back from sql an xml string of multiple addresses, here is an example of what is returning back:

<Addresses>
  <Address>
    <LetterQueueOID>2</LetterQueueOID>
    <Address1>115 MORNINGVIEW TRL</Address1>
    <Address2>SCARBOROUGH, </Address2>
    <Address3>M1B5L2</Address3>
    <City>SCARBOROUGH</City>
    <PostalCode>M1B5L2</PostalCode>
  </Address>
  <Address>
    <LetterQueueOID>1</LetterQueueOID>
    <Address1>GD PO BOX 685</Address1>
    <Address2>THORNBURY, ON</Address2>
    <Address3>N0H2P0</Address3>
    <City>THORNBURY</City>
    <ProvinceOrState>ON</ProvinceOrState>
    <CountryCode>Ca</CountryCode>
    <PostalCode>N0H2P0</PostalCode>
  </Address>
</Addresses>

I want to use LINQ to query this xml string for specific LetterQueueOID's , i.e.
Select Address(as a string) where LetterQueueOID = 2.

All I've figured out is that i can use XDocument, but i can't figure out exactly how to get what I want:

XDocument addresses = XDocument.Parse((string)returnScalar);
IEnumerable<XElement> items = addresses.Root.Elements("Address").ToList();

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

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

发布评论

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

评论(2

素食主义者 2024-10-10 10:24:28

尝试使用Where 扩展方法的lambda 表达式。这里我得到的是 id 为 2 的地址,仅作为示例

XDocument addresses = XDocument.Parse((string)returnScalar);
var address = addresses.Root.Elements("Address").Where(address => address.Element("LetterQueueOID").Value == "2").FirstOrDefault();

try a lambda expression with the Where extension method. here i'm getting the address with id 2, just as an example

XDocument addresses = XDocument.Parse((string)returnScalar);
var address = addresses.Root.Elements("Address").Where(address => address.Element("LetterQueueOID").Value == "2").FirstOrDefault();
巴黎盛开的樱花 2024-10-10 10:24:28

好吧,您可以这样做:

var matches = addresses
                 .Root
                 .Elements("Address")
                 .Where(addr => (string) addr.Element("LetterQueueOID") == "2")

但是,这将为您提供所有相关的

元素。您说您想以字符串形式返回地址 - 但以什么格式?

您可以像这样连接字符串:

var matches = addresses
                 .Root
                 .Elements("Address")
                 .Where(addr => (string) addr.Element("LetterQueueOID") == "2")
                 .Select(addr => (string) addr.Element("Address1") + " "
                                 (string) addr.Element("Address2") + " "
                                 /* etc */);

Well, you can do:

var matches = addresses
                 .Root
                 .Elements("Address")
                 .Where(addr => (string) addr.Element("LetterQueueOID") == "2")

However, that will get you all the relevant <Address> elements. You say you want to get the address back as a string - but in what format?

You could concatenate the strings like this:

var matches = addresses
                 .Root
                 .Elements("Address")
                 .Where(addr => (string) addr.Element("LetterQueueOID") == "2")
                 .Select(addr => (string) addr.Element("Address1") + " "
                                 (string) addr.Element("Address2") + " "
                                 /* etc */);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文