Linq to XML 联接并添加数据?

发布于 2024-10-27 10:36:37 字数 1243 浏览 5 评论 0原文

给定这样的 XML:

<Root>
    <Element>
        <Id>1</Id>
    </Element>
    <Element>
        <Id>2</Id>
    </Element>
    <Element>
        <Id>3</Id>
    </Element>
</Root>

以及包含此数据的 IEnumerable:

[0] = { Id = 1, Data = 429 }
[1] = { Id = 2, Data = 271 }
[2] = { Id = 3, Data = 328 }

是否有一种优雅的方法使用 LINQ to XML 将 IEnumerable 数据与基于公共 Id 的 XML 连接起来,而无需单独查询每个元素来链接数据与?

所以结果看起来像这样:

<Root>
    <Element>
        <Id>1</Id>
        <Data>429</Data>
    </Element>
    <Element>
        <Id>2</Id>
        <Data>271</Data>
    </Element>
    <Element>
        <Id>3</Id>
        <Data>328</Data>
    </Element>
</Root>

我唯一能想到的就是这样的东西,但我希望有一个更干净的方法:

foreach(var d in MyIEnumerable)
{
    XElement element = (from x in MyXDoc.Elements("Element")
                        where x.Element("Id").Value == d.Id
                        select x).Single();

    element.Add(new XElement("Data", d.Data));
}

Given XML like this:

<Root>
    <Element>
        <Id>1</Id>
    </Element>
    <Element>
        <Id>2</Id>
    </Element>
    <Element>
        <Id>3</Id>
    </Element>
</Root>

And an IEnumerable with this data:

[0] = { Id = 1, Data = 429 }
[1] = { Id = 2, Data = 271 }
[2] = { Id = 3, Data = 328 }

Is there an elegant way using LINQ to XML to join the IEnumerable data with the XML based on the common Id without individually querying for each element to link the data with?

So the result would look like this:

<Root>
    <Element>
        <Id>1</Id>
        <Data>429</Data>
    </Element>
    <Element>
        <Id>2</Id>
        <Data>271</Data>
    </Element>
    <Element>
        <Id>3</Id>
        <Data>328</Data>
    </Element>
</Root>

The only thing I can come up with is something like this, but I was hoping there was a cleaner method:

foreach(var d in MyIEnumerable)
{
    XElement element = (from x in MyXDoc.Elements("Element")
                        where x.Element("Id").Value == d.Id
                        select x).Single();

    element.Add(new XElement("Data", d.Data));
}

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

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

发布评论

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

评论(2

许久 2024-11-03 10:36:37

一种方法是使用 Tuple。 (Element 是包含 ID 和数据的类。)

var output = from el in els
             join xmlEl in xdoc.Root.Elements("Element") on el.Id equals int.Parse(xmlEl.Element("Id").Value)
             select new Tuple<Element, XElement>(el, xmlEl);

foreach(var item in output)
{
    item.Item2.Add(new XElement("Data", item.Item1.Data));
}

如果您没有元组,那么您可以创建一个匿名类。

select new {ObjEl = el, XmlEl = xmlEl}

One way is to use a Tuple<Element, XElement>. (The Element is the class that has ID and Data in it.)

var output = from el in els
             join xmlEl in xdoc.Root.Elements("Element") on el.Id equals int.Parse(xmlEl.Element("Id").Value)
             select new Tuple<Element, XElement>(el, xmlEl);

foreach(var item in output)
{
    item.Item2.Add(new XElement("Data", item.Item1.Data));
}

If you don't have tuples then you can create an anonymous class instead.

select new {ObjEl = el, XmlEl = xmlEl}
撧情箌佬 2024-11-03 10:36:37

您也许可以执行以下操作:

var elements = MyXDoc.Elements("Element");
elements.Apply(e => e.Add(new XElement("Data", MyIEnumerable.FirstOrDefault(d => d.Id == e.Element("Id").Value).Data)));            

其中 Apply 扩展方法定义为:

public static class Extensions
{
    public static void Apply<T>(this IEnumerable<T> enumerable, Action<T> action)
    {
        foreach (var item in enumerable)
        {
            action(item);
        }
    }
}

You might be able to do something like:

var elements = MyXDoc.Elements("Element");
elements.Apply(e => e.Add(new XElement("Data", MyIEnumerable.FirstOrDefault(d => d.Id == e.Element("Id").Value).Data)));            

Where the Apply extension method is defined as:

public static class Extensions
{
    public static void Apply<T>(this IEnumerable<T> enumerable, Action<T> action)
    {
        foreach (var item in enumerable)
        {
            action(item);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文