XML 到 LINQ 元素选择

发布于 2025-01-03 10:48:24 字数 977 浏览 0 评论 0原文

我有一个以下格式的 xml 文档。

<?xml version="1.0" encoding="UTF-8" ?>

<Rows>
<Row>    
    <Field Name='PhysicalLocation'>11;#West</Field>
    <Field Name='ID'>3327</Field>
</Row>
</Rows>

我正在尝试用它进行 linq 选择。

我已经尝试过以下方法。

XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");

var query = from item in xmlDoc.Descendants("Rows").Elements()
                    select new { ID = item.Attribute("ID").Value, Value = item.Attribute("PhysicalLocation").Value };

而且

XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");

var query = from item in xmlDoc.Descendants("Rows").Elements()
                    select new { ID = item.Element("ID"), Value = item.Element("PhysicalLocation") };

在这两种情况下我似乎都达不到要求。它正在生成预期数量的行,但未填充值。

有人能指出我正确的方向吗?我缺少什么?

I have an xml document in the following format.

<?xml version="1.0" encoding="UTF-8" ?>

<Rows>
<Row>    
    <Field Name='PhysicalLocation'>11;#West</Field>
    <Field Name='ID'>3327</Field>
</Row>
</Rows>

And am attempting to do a linq selection with it.

I have tried the following.

XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");

var query = from item in xmlDoc.Descendants("Rows").Elements()
                    select new { ID = item.Attribute("ID").Value, Value = item.Attribute("PhysicalLocation").Value };

And also

XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");

var query = from item in xmlDoc.Descendants("Rows").Elements()
                    select new { ID = item.Element("ID"), Value = item.Element("PhysicalLocation") };

And in both cases I seem to be coming up short. It is generating the expected amount of rows but the values are not being filled.

Could anyone point me in the right direction? What am I missing?

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

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

发布评论

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

评论(4

執念 2025-01-10 10:48:25

您没有名为“PhysicalLocation”或“ID”的属性。您只有名为“名称”的属性。

您需要在“name”属性的值上添加一个 where 子句来查找您的 ID 和 PhysicalLocation

you don't have an attribute called 'PhysicalLocation' or 'ID'. you only have attributes called 'Name'.

You need to add a where clause on the value of the 'name' attribute to find your ID and PhysicalLocation

人海汹涌 2025-01-10 10:48:25

我认为这应该可以解决问题:

var xDoc = XDocument.Parse(
@"<?xml version='1.0' encoding='UTF-8' ?> 

<Rows> 
    <Row>     
        <Field Name='PhysicalLocation'>11;#West</Field> 
        <Field Name='ID'>3327</Field> 
    </Row> 
</Rows>");

var res = from row in xDoc.Root.Elements("Row")
          select new
          {
              ID = (int)row.Elements("Field").Single(e => (string)e.Attribute("Name") == "ID"),
              PhysicalLocaltion = (string)row.Elements("Field").Single(e => (string)e.Attribute("Name") == "PhysicalLocation"),
          };

foreach (var r in res)
{
    Console.WriteLine(r);
}

这是循环打印的结果:

{ ID = 3327, PhysicalLocaltion = 11;#West }

I think this should do the trick:

var xDoc = XDocument.Parse(
@"<?xml version='1.0' encoding='UTF-8' ?> 

<Rows> 
    <Row>     
        <Field Name='PhysicalLocation'>11;#West</Field> 
        <Field Name='ID'>3327</Field> 
    </Row> 
</Rows>");

var res = from row in xDoc.Root.Elements("Row")
          select new
          {
              ID = (int)row.Elements("Field").Single(e => (string)e.Attribute("Name") == "ID"),
              PhysicalLocaltion = (string)row.Elements("Field").Single(e => (string)e.Attribute("Name") == "PhysicalLocation"),
          };

foreach (var r in res)
{
    Console.WriteLine(r);
}

Here is the result printed by the loop:

{ ID = 3327, PhysicalLocaltion = 11;#West }
野侃 2025-01-10 10:48:25

试试这个:

        var xd = XDocument.Load("C:\\manifest.xml");
        var query = xd.Root.Descendants("Row").Elements("Field")
            .Select(s => new
            {
                Name = (string)s.Attribute("Name"),
                Value = s.Value
            });

上面的代码循环遍历每个“Row”元素,然后读取“Field”元素数据。它将返回以下匿名列表:

Name = PhysicalLocation
Value = 11;#West
Name = ID
Value = 3327 

要循环查询,可以使用以下代码:

        var sb = new StringBuilder();
        foreach (var i in query)
        {
            sb.Append("\n Name = ").Append(i.Name).Append("\n Value = ").Append(i.Value);
        }

最后,要通过 Name 查找 Field 元素值,可以使用以下查询:

var query2 = query.Where(w => w.Name == "ID").Single().Value;

Try this:

        var xd = XDocument.Load("C:\\manifest.xml");
        var query = xd.Root.Descendants("Row").Elements("Field")
            .Select(s => new
            {
                Name = (string)s.Attribute("Name"),
                Value = s.Value
            });

The above code, loops through each "Row" element, then reads the "Field" element data. It will return the following anonymous list:

Name = PhysicalLocation
Value = 11;#West
Name = ID
Value = 3327 

To loop through the query, you can use the following code:

        var sb = new StringBuilder();
        foreach (var i in query)
        {
            sb.Append("\n Name = ").Append(i.Name).Append("\n Value = ").Append(i.Value);
        }

Finally, to find the Field element value by Name, you can use the following query:

var query2 = query.Where(w => w.Name == "ID").Single().Value;
以酷 2025-01-10 10:48:24

尝试这样的查询怎么样:

var query =
    from item in xmlDoc.Descendants("Rows").Elements()
    let values = item.Elements("Field")
        .ToDictionary(x => x.Attribute("Name").Value, x => x.Value)
    select new
    {
        ID = values["ID"],
        Value = values["PhysicalLocation"],
    };

How about trying a query like this:

var query =
    from item in xmlDoc.Descendants("Rows").Elements()
    let values = item.Elements("Field")
        .ToDictionary(x => x.Attribute("Name").Value, x => x.Value)
    select new
    {
        ID = values["ID"],
        Value = values["PhysicalLocation"],
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文