简体   繁体   English

在C#中使用Linq to XML解析XML的示例

[英]Examples of using Linq to XML in c# for parsing XML

I posed a question last night regarding how to parse through repeating XML structures. 昨晚我提出了一个有关如何通过重复XML结构进行解析的问题。 The XML has two levels that repeat and the second level contains elements that I need to store elsewhere. XML有两个重复的层次,第二个层次包含我需要存储在其他地方的元素。

The question was marked as a duplicate and pointed to an accepted answer that stated 'use Linq to XML' basically. 该问题被标记为重复,并指出了一个可接受的答案,该答案基本上是“使用Linq到XML”。

While I appreciate the answer, the bottom of my question stated I've tried to understand how to use LINQ to XML, but couldn't figure out how to access the second level data. 当我欣赏答案时,我在问题的最下方指出,我试图理解如何使用LINQ to XML,但无法弄清楚如何访问第二级数据。

Are there any easy to understand examples or tutorials on how to use LINQ to XML, or any other method for that matter, to take the information in an XML document and be able to loop through it? 是否有任何简单易懂的示例或教程,说明如何使用LINQ to XML或其他任何方法来获取XML文档中的信息并能够遍历它?

I've done this in Perl previously and it basically sucks in the XML and creates a large object you can iterate through. 我之前在Perl中已经做到了,它基本上吸收了XML,并创建了一个可以迭代的大对象。

Link to original question: Parsing XML with C Sharp 链接到原始问题: 使用C Sharp解析XML

They say "Practice make Perfect". 他们说“实践使完美”。 Sometimes doing something a number of times is the best method for learning. 有时做几次是学习的最佳方法。 Use the linq method "ToList()" like in the code below 像下面的代码一样使用linq方法“ ToList()”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<Data>" +
                  "<Report>" +
                    "<Machine name=\"hostA\">" +
                      "<MachineInfo location=\"LA\">" +
                        "<function name=\"run\">Late</function>" +
                        "<function name=\"status\">Complete</function>" +
                        "<function name=\"date\">2015-06-14</function>" +
                      "</MachineInfo>" +
                      "<RepItem name=\"1488\" direction=\"NS\">" +
                        "<Desc>None Found</Desc>" +
                        "<Status Int=\"A12\">Uplink</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1489\" direction=\"S\">" +
                        "<Desc>31Ghz Ant at 285ft.</Desc>" +
                        "<Status Int=\"D5\">Active</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1438\" direction=\"W\">" +
                        "<Desc>West N. Oc. Backup</Desc>" +
                        "<Status Int=\"A11\">Disabled</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1141\" direction=\"SE\">" +
                        "<Desc>MDT Co.</Desc>" +
                        "<Status Int=\"B7\">Active</Status>" +
                      "</RepItem>" +
                    "</Machine>" +
                    "<Machine name=\"hostB\">" +
                      "<MachineInfo location=\"E. LA\">" +
                        "<function name=\"run\">Late</function>" +
                        "<function name=\"status\">Complete</function>" +
                        "<function name=\"date\">2015-06-14</function>" +
                      "</MachineInfo>" +
                      "<RepItem name=\"1488\" direction=\"NS\">" +
                        "<Desc>None Found</Desc>" +
                        "<Status Int=\"A12\">Down</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1489\" direction=\"S\">" +
                        "<Desc>31Ghz Ant at 285ft.</Desc>" +
                        "<Status Int=\"D5\">Active</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1438\" direction=\"W\">" +
                        "<Desc>West N. Oc. Backup</Desc>" +
                        "<Status Int=\"A11\">Disabled</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1141\" direction=\"SE\">" +
                        "<Desc>MDT Co.</Desc>" +
                        "<Status Int=\"B7\">Active</Status>" +
                      "</RepItem>" +
                    "</Machine>" +
                  "</Report>" +
                "</Data>";

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("Machine")
                .Select(x => new {
                    name = x.Attribute("name").Value,
                    info = new {
                        machineInfo = x.Element("MachineInfo").Attribute("location").Value,
                        functions = x.Element("MachineInfo").Elements("function").Select(y => y.Value).ToList()
                    },
                    repItems = x.Elements("RepItem")
                        .Select(y => new {
                            name = y.Attribute("name").Value, 
                            direction = y.Attribute("direction").Value,
                            description = y.Element("Desc").Value,
                            status = y.Element("Status").Value,
                            index =  y.Element("Status").Attribute("Int").Value
                        }).ToList()
                })
                .ToList();
        }
    }
}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM