C# 如何使用 foreach 和 LINQ 构建 XDocument?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1002348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:16:19  来源:igfitidea点击:

How to build an XDocument with a foreach and LINQ?

c#xmllinq

提问by Edward Tanguay

I can use XDocument to build the following file which works fine:

我可以使用 XDocument 来构建以下工作正常的文件:

XDocument xdoc = new XDocument
(
    new XDeclaration("1.0", "utf-8", null),
    new XElement(_pluralCamelNotation,
        new XElement(_singularCamelNotation,
            new XElement("id", "1"),
            new XElement("whenCreated", "2008-12-31")
        ),
        new XElement(_singularCamelNotation,
            new XElement("id", "2"),
            new XElement("whenCreated", "2008-12-31")
            )
        )
);

However, I need to build the XML file by iterating through a collectionlike this:

但是,我需要通过遍历这样的集合来构建 XML 文件:

XDocument xdoc = new XDocument
(
    new XDeclaration("1.0", "utf-8", null));

foreach (DataType dataType in _dataTypes)
{
    XElement xelement = new XElement(_pluralCamelNotation,
        new XElement(_singularCamelNotation,
        new XElement("id", "1"),
        new XElement("whenCreated", "2008-12-31")
    ));
    xdoc.AddInterally(xelement); //PSEUDO-CODE
}

There is Add, AddFirst, AddAfterSelf, AddBeforeSelf, but I could get none of them to work in this context.

AddAddFirstAddAfterSelfAddBeforeSelf,但我无法让它们在这种情况下工作。

Is an iteration with LINQ like this possible?

像这样的 LINQ 迭代可能吗?

Answer:

回答:

I took Jimmy's code suggestion with the root tag, changed it up a bit and it was exactly what I was looking for:

我采用了带有根标记的 Jimmy 的代码建议,对其进行了一些更改,这正是我想要的:

var xdoc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement(_pluralCamelNotation,
        _dataTypes.Select(datatype => new XElement(_singularCamelNotation,
            new XElement("id", "1"),
            new XElement("whenCreated", "2008-12-31")
        ))
    )
);

Marc Gravell posted a better answer to this on this StackOverflow question.

Marc Gravell在这个 StackOverflow 问题上发布了一个更好的答案。

采纳答案by Jimmy

You need a root element.

你需要一个根元素。

var xdoc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement("Root",
        _dataTypes.Select(datatype => new XElement(datatype._pluralCamelNotation,
            new XElement(datatype._singlarCamelNotation),
            new XElement("id", "1"),
            new XElement("whenCreated", "2008-12-31")
        ))
    )
);

回答by BFree

What's wrong with the simple Add method?

简单的Add 方法有什么问题?

回答by Dustin Campbell

If I'm not mistaken, you should be able to use XDocument.Add():

如果我没记错的话,您应该可以使用 XDocument.Add():

XDocument xdoc = new XDocument
(
    new XDeclaration("1.0", "utf-8", null));

foreach (DataType dataType in _dataTypes)
{
    XElement xelement = new XElement(_pluralCamelNotation,
        new XElement(_singularCamelNotation,
        new XElement("id", "1"),
        new XElement("whenCreated", "2008-12-31")
    ));
    xdoc.Add(xelement);
}

回答by Kiks

I know it's very very old post but I stumbled across this today trying to solve the same problem. You have to add the element to the Root of the document:

我知道这是非常非常古老的帖子,但我今天偶然发现了这个试图解决同样的问题。您必须将元素添加到文档的根:

xdoc.Root.Add(xelement);