使用 LINQ to XML,.NET 让生成 XML 文件变得和直接写 XML 一样轻松

由 .NET Framework 3.5 引入,并依然在 .NET Core 中发扬光大的 LINQ to XML 让编写 XML 文件变得非常轻松。


使用 XElementXAttribute 我们能够完整构造一个 XML 出来。为了能直观地体会到优势,我写一个最简单的例子:

var root = new XElement("Root",
    new XAttribute("Attribute", "Walterlv"),
    new XElement("Node", "Content")
);

构造出来的 XML 将是这样的:

<Root Attribute="Walterlv">
  <Node>Content</Node>
</Root>

是不是觉得包括行的安排和缩进在内,都和 XML 一样简单?

我们来看一个更复杂的例子,这是直接在编写一个 NuGet 的 nuspec 文件:

var xmlns = "http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd";
var root = new XElement("package",
    new XAttribute(XNamespace.Xmlns.ToString(), xmlns),
    new XElement("metadata",
        new XElement("id", "MSTestEnhancer"),
        new XElement("version", "1.6.0"),
        new XElement("authors", "walterlv"),
        new XElement("owners", "walterlv"),
        new XElement("requireLicenseAcceptance", "false"),
        new XElement("licenseUrl", "https://github.com/dotnet-campus/MSTestEnhancer/blob/master/LICENSE"),
        new XElement("projectUrl", "https://dotnet-campus.github.io/mstest-enhancer"),
        new XElement("iconUrl", "https://dotnet-campus.github.io/mstest-enhancer/icon.png"),
        new XElement("description", "MSTestEnhancer helps you to write unit tests without naming any method. You can write method contract descriptions instead of writing confusing test method name when writing unit tests."),
        new XElement("releaseNotes", "Support passing null into WithArgument method."),
        new XElement("copyright", "Copyright (c) 2018 dotnet职业技术学院"),
        new XElement("repository",
            new XAttribute("type", "git"),
            new XAttribute("url", "https://github.com/dotnet-campus/MSTestEnhancer.git")),
        new XElement("dependencies", dependencies.Select(group => new XElement("group",
            new XAttribute("targetFramework", group.Key), group.Value.Select(x =>
                new XElement("dependency",
                    new XAttribute("id", x.id),
                    new XAttribute("version", x.version),
                    new XAttribute("exclude", x.exclude)))))
        )
    ));
var document = new XDocument(root);
document.Save(@"C:\Users\walterlv\Desktop\Walterlv.Demo.nuspec");

其中的 dependencies 集合我写在了其他地方,这样更像是动态生成,而不是仅仅为了给一个例子。

var dependencies = new Dictionary<string, (string id, string version, string exclude)[]>
{
    [".NETFramework4.5"] = new[]
    {
        ("MSTest.TestFramework", "1.2.0", "Build,Analyzers"),
        ("System.ValueTuple", "4.4.0", "Build,Analyzers"),
    },
    [".NETFramework4.7"] = new[]
    {
        ("MSTest.TestFramework", "1.2.0", "Build,Analyzers"),
    },
    [".NETStandard2.0"] = new[]
    {
        ("MSTest.TestFramework", "1.2.0", "Build,Analyzers"),
    }
};

生成的 nuspec 文件非常像 NuGet 的原生 nuspec 文件。

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>MSTestEnhancer</id>
    <version>1.6.0</version>
    <authors>walterlv</authors>
    <owners>walterlv</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <licenseUrl>https://github.com/easiwin/MSTestEnhancer/blob/master/LICENSE</licenseUrl>
    <projectUrl>https://easiwin.github.io/mstest-enhancer</projectUrl>
    <iconUrl>https://easiwin.github.io/mstest-enhancer/icon.png</iconUrl>
    <description>MSTestEnhancer helps you to write unit tests without naming any method. You can write method contract descriptions instead of writing confusing test method name when writing unit tests.</description>
    <releaseNotes>Support passing null into WithArgument method.</releaseNotes>
    <copyright>Copyright (c) 2018 dotnet职业技术学院</copyright>
    <repository type="git" url="https://github.com/easiwin/MSTestEnhancer.git" />
    <dependencies>
      <group targetFramework=".NETFramework4.5">
        <dependency id="MSTest.TestFramework" version="1.2.0" exclude="Build,Analyzers" />
        <dependency id="System.ValueTuple" version="4.4.0" exclude="Build,Analyzers" />
      </group>
      <group targetFramework=".NETFramework4.7">
        <dependency id="MSTest.TestFramework" version="1.2.0" exclude="Build,Analyzers" />
      </group>
      <group targetFramework=".NETStandard2.0">
        <dependency id="MSTest.TestFramework" version="1.2.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

参考资料

本文会经常更新,请阅读原文: https://walterlv.com/post/create-xml-using-linq-to-xml.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (walter.lv@qq.com)