繁体   English   中英

使用格式和缩进将 XElement 添加到 XML 文件

[英]Add XElement to XML file with formatting and indenting

XML

源 XML

<!-- The comment -->
<Root xmlns="http://www.namespace.com">
    <FirstElement>
    </FirstElement>

    <SecondElement>
    </SecondElement>
</Root>

所需的 XML

<!-- The comment -->
<Root xmlns="http://www.namespace.com">
    <FirstElement>
    </FirstElement>

    <SecondElement>
    </SecondElement>

    <ThirdElement>
        <FourthElement>thevalue</FourthElement>
    </ThirdElement>
</Root>

现在我的输出 XML 是

<!-- The comment -->
<Root xmlns="http://www.namespace.com">
    <FirstElement>
    </FirstElement>

    <SecondElement>
    </SecondElement><ThirdElement><FourthElement>thevalue</FourthElement></ThirdElement>
</Root>

请注意,我需要使用LoadOptions.PreserveWhitespace加载 XML,因为我需要保留所有空格(客户需要)。 所需的输出是在“根”的最后一个子元素之后放置 2 个换行符,并添加适当的缩进

<ThirdElement>
    <FourthElement>thevalue</FourthElement>
</ThirdElement>

任何想法如何实现这一点?

代码

var xDoc = XDocument.Load(sourceXml, LoadOptions.PreserveWhitespace); //need to preserve all whitespaces
var mgr = new XmlNamespaceManager(new NameTable());
var ns = xDoc.Root.GetDefaultNamespace();
mgr.AddNamespace("ns", ns.NamespaceName);

if (xDoc.Root.HasElements)
{
    xDoc.Root.Elements().Last().AddAfterSelf(new XElement(ns + "ThirdElement", new XElement(ns + "FourthElement", "thevalue")));

    using (var xw = XmlWriter.Create(outputXml, new XmlWriterSettings() { OmitXmlDeclaration = true })) //omit xml declaration
        xDoc.Save(xw);
}

理想情况下,您应该向您的客户解释这真的不重要。

但是,如果您真的需要处理空白,我会注意到XText正是您所需要的。 这是另一个表示文本节点的XObject ,可以作为内容的一部分散布。 这可能是比字符串操作更好的方法。

例如:

doc.Root.Add(
    new XText("\n\t"),
    new XElement(ns + "ThirdElement",
        new XText("\n\t\t"),
        new XElement(ns + "FourthElement", "thevalue"),
        new XText("\n\t")),
    new XText("\n"));

请参阅此演示

我的解决方案是在保存之前通过重新解析文档来美化。

string content = XDocument.Parse(xDoc.ToString()).ToString();
File.WriteAllText(file, content, Encoding.UTF8);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM