X文档后代的后代

发布于 2024-10-19 19:32:23 字数 678 浏览 1 评论 0原文

<Tickets>
<Extract_Date>2011-02-25 00:00:00</Extract_Date>
<Incidents>
  <Ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Ticket_Number>INC000000578057</Ticket_Number>
    <Status>
      <Value>Cancelled</Value>
    <Reason>Cancelled by user</Reason>
  </Status>
</Ticket>

我可以通过以下方式获取ticket_number:

var q1 = from c in xmlDoc.Descendants("Ticket")
                   select new
                   {
                       Ticket_Number = (string)c.Element("Ticket_Number"),
                   };

如何获取Reason?

<Tickets>
<Extract_Date>2011-02-25 00:00:00</Extract_Date>
<Incidents>
  <Ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Ticket_Number>INC000000578057</Ticket_Number>
    <Status>
      <Value>Cancelled</Value>
    <Reason>Cancelled by user</Reason>
  </Status>
</Ticket>

I can get ticket_number OK with:

var q1 = from c in xmlDoc.Descendants("Ticket")
                   select new
                   {
                       Ticket_Number = (string)c.Element("Ticket_Number"),
                   };

How to get Reason also?

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

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

发布评论

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

评论(2

伤感在游骋 2024-10-26 19:32:23

这应该有效:

var q1 = from c in xmlDoc.Descendants("Ticket")
    select new
    {
        Ticket_Number = (string)c.Element("Ticket_Number"),
        Reason = (string)c.Element("Status").Element("Reason")
    };

This should work:

var q1 = from c in xmlDoc.Descendants("Ticket")
    select new
    {
        Ticket_Number = (string)c.Element("Ticket_Number"),
        Reason = (string)c.Element("Status").Element("Reason")
    };
贱贱哒 2024-10-26 19:32:23
//if you have exactly one <Ticket> with exactly one <Reason>
string strReason = xmlDoc.Descendants("Ticket").Single()
    .Descendants("Reason").Single().Value;

//if you have one or multiple <Ticket> elements, 
//each with exactly one <Reason> element
string[] astrReasons = xmlDoc.Descendants("Ticket")
    .Select(ticket => ticket.Descendants("Reason").Single().Value).ToArray();

//if you have one or multiple <Ticket> elements, 
//each with one or multiple <Reason> elements
string[] astrReasons2 = xmlDoc.Descendants("Ticket")
    .SelectMany(ticket => ticket.Descendants("Reason")
    .Select(reason => reason.Value)).ToArray();
//if you have exactly one <Ticket> with exactly one <Reason>
string strReason = xmlDoc.Descendants("Ticket").Single()
    .Descendants("Reason").Single().Value;

//if you have one or multiple <Ticket> elements, 
//each with exactly one <Reason> element
string[] astrReasons = xmlDoc.Descendants("Ticket")
    .Select(ticket => ticket.Descendants("Reason").Single().Value).ToArray();

//if you have one or multiple <Ticket> elements, 
//each with one or multiple <Reason> elements
string[] astrReasons2 = xmlDoc.Descendants("Ticket")
    .SelectMany(ticket => ticket.Descendants("Reason")
    .Select(reason => reason.Value)).ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文