使用 C# LINQ to XML 的 XML

发布于 2024-08-06 14:04:06 字数 704 浏览 3 评论 0原文

以下是 XML 文档的示例。

<People>
   <Person>
       <Name>ABC </Name>
       <SSN>111111</SSN>
       <Address>asdfg</Address>
   </Person>
</People>

我需要获取标签名称,但不需要获取标签名称之间的值。也就是说,在 person 标签下,我应该获取 NameSSNAddress 节点,而不是 ABC 节点>、111111asdfg 值。

我需要使用 LINQ to XML 并在 < a href="http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29" rel="nofollow noreferrer">C#。

我该怎么做呢?

Following is the example of an XML document.

<People>
   <Person>
       <Name>ABC </Name>
       <SSN>111111</SSN>
       <Address>asdfg</Address>
   </Person>
</People>

I need to get the tag names but not the values between the tag names. That is, under the person tag, I should grab the Name, SSN, and Address nodes and not the ABC, 111111, and asdfg values.

I need to use LINQ to XML and query it in C#.

How can I do it?

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

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

发布评论

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

评论(4

可遇━不可求 2024-08-13 14:04:06

这将名称作为字符串列表返回:

var doc = XDocument.Parse(@"<People>
   <Person>
       <Name>ABC </Name>
       <SSN>111111</SSN>
       <Address>asdfg</Address>
   </Person>
</People>"
);

var list = doc.Root.Element("Person").Descendants()
              .Select(node => node.Name.LocalName).ToList();

如果您使用 XElement 而不是 XDocument,则可以从上面的代码中删除 .Root 并获得正确的结果。

This returns the names as a list of strings:

var doc = XDocument.Parse(@"<People>
   <Person>
       <Name>ABC </Name>
       <SSN>111111</SSN>
       <Address>asdfg</Address>
   </Person>
</People>"
);

var list = doc.Root.Element("Person").Descendants()
              .Select(node => node.Name.LocalName).ToList();

In case you're using an XElement instead of an XDocument, you can remove the .Root from the above code and get the correct results.

离不开的别离 2024-08-13 14:04:06

创建一个类

public class Person
{
     public string Name {get; set;}
     public int SSN {get; set;}
     public string Address {get; set;}
}

并这样创建一个新人;

List<Person> NewPersons = new List<Person>();
XDocument doc = XDocument.Load(xmlFile);     
foreach(XElement xElem in doc.Descendants("Person"))
{
    NewPersons.Add(new Person
            {
                Name = xElem. Element("Name").Value,
                SSN = xElem.Element("SSN").Value,
                Address = xElem.Element("Address").Value,
            });
}

Create a class

public class Person
{
     public string Name {get; set;}
     public int SSN {get; set;}
     public string Address {get; set;}
}

And create a new person this way;

List<Person> NewPersons = new List<Person>();
XDocument doc = XDocument.Load(xmlFile);     
foreach(XElement xElem in doc.Descendants("Person"))
{
    NewPersons.Add(new Person
            {
                Name = xElem. Element("Name").Value,
                SSN = xElem.Element("SSN").Value,
                Address = xElem.Element("Address").Value,
            });
}
↙厌世 2024-08-13 14:04:06

你可以这样得到它...

string xml = "<People> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> </People>";

XElement xmlData = XElement.Parse(xml);

foreach(XElement xmlPerson in xmlData.Elements("Person"))
{
    List<string> TagsForThisPerson = new List<string>();
    foreach(XElement xmlTag in xmlPerson.Elements())
    {
        TagsForThisPerson.Add(xmlTag.Name.ToString());
    }
    TagsForThisPerson.Dump();
}

You can get it this way...

string xml = "<People> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> </People>";

XElement xmlData = XElement.Parse(xml);

foreach(XElement xmlPerson in xmlData.Elements("Person"))
{
    List<string> TagsForThisPerson = new List<string>();
    foreach(XElement xmlTag in xmlPerson.Elements())
    {
        TagsForThisPerson.Add(xmlTag.Name.ToString());
    }
    TagsForThisPerson.Dump();
}
烟雨扶苏 2024-08-13 14:04:06

下面列出了用于获取名称的简单 LINQ 语法。它假设您已将 XML 加载到名为 doc 的 XDocument 变量中。

var nodeNames = from node in doc.Descendants("Person").First().Descendants()
                select node.Name.LocalName;

它只看第一个人。如果 XML 文档中有多个节点,则名称列表可能不是您想要的(没有理由一遍又一遍地重复节点的所有名称)。这样,您就可以获得第一个人的节点名称列表,但它确实假设第一个人有完整的名称列表。如果它们有所不同,您将需要从所有节点构建一个不同的列表。

Simple LINQ syntax to get the names is listed below. It assumes you have the XML loaded in a XDocument variable named doc.

var nodeNames = from node in doc.Descendants("Person").First().Descendants()
                select node.Name.LocalName;

It only looks at the first person. If you have more than one in the XML document, the list of names is probably not what you would want (no reason to repeat all the names of the nodes over and over). This way, you get a list of just the node names for the first person, but it does assume that the first one would have a complete list of names. If they vary, you would need to build a distinct list from all the nodes.

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