6

我还没有找到一种方法,使用 .NET 的 XmlWriter 和关联的 XmlWriterSettings 以缩进形式格式化 XML 字符串,就像 Visual Studio 使用其自动格式化命令(Ctrl-E Ctrl-D,或者,取决于键盘映射)那样, Ctrl-K Ctrl-D)。

我想这样做是因为我习惯性地在 VS 中自动格式化所有文件,包括代码和 .config 文件。我有一个更新 .config 文件的安装程序应用程序,我想查看实际差异而不是更改整个文档。

我还没有探索自动格式的所有不同格式选项,但我喜欢每个 XML 属性位于单独的行上,第一个与开始标记在同一行,随后的与第一个对齐,像这样:

<asset assetId="12345"
       bucket="default"
       owner="nobody">
  <file path="\\localhost\share\assetA.mov"/>
  <metadata metadataId="23456"
            key="asset_type"
            value="video"/>
</asset>

我尝试使用 XmlWriterSettings 属性“NewLineHandling = NewLineHandling.None”和“NewLineOnAttributes = true”进行格式化,但这会将第一个属性放在开始标记下方,并且无论元素名称中的字符数如何,所有属性都具有相同的缩进,像这样:

<asset
  assetId="12345"
  bucket="default"
  owner="nobody">
  <file
    path="\\localhost\share\assetA.mov" />
  <metadata metadataId="23456"
    key="asset_type"
    value="video" />
</asset>

请注意,标准 XmlWriter 还以“ />”(斜杠前的额外空格)结束仅属性元素,我不喜欢这但不确定这是否是 XML 标准。我认为 Visual Studio 使用开发人员随时可用的相同 API 选项,但我还没有找到那些神奇的设置。无论如何,这是我的格式方法:

public static string FormatXml( string xmlString, bool indented )
{
    using ( TextReader textReader = new StringReader( xmlString ) )
    using ( XmlReader xmlReader = new XmlTextReader( textReader ) )
    {
        using ( TextWriter textWriter = new StringWriter() )
        {
            var settings = new XmlWriterSettings();
            if ( indented )
            {
               settings.Indent = true;
               settings.IndentChars = "  ";
               settings.NewLineOnAttributes = true;
               settings.NewLineHandling = NewLineHandling.None;
            }
            using ( var xmlWriter = XmlWriter.Create( textWriter, settings ) )
            {
                while ( xmlReader.Read() )
                    xmlWriter.WriteNode( xmlReader, false );
            }
            return textWriter.ToString();
        }
    }
}
4

2 回答 2

1

我误解了这个问题。实际上我不知道是否有办法按照您显示的方式对齐属性。您可以尝试自己实现它,如下所示:

    public static string FormatXml(string xmlString, bool indented)
    {
        using (TextReader textReader = new StringReader(xmlString))
        using (XmlReader xmlReader = new XmlTextReader(textReader))
        {
            using (TextWriter textWriter = new StringWriter())
            {
                string indent = "";
                string attributeIndent = "";

                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType == XmlNodeType.Element)
                    {
                        attributeIndent = "";
                        string element = xmlReader.Name;
                        textWriter.Write("{0}<{1}", indent, element);

                        if (!xmlReader.HasAttributes)
                            textWriter.WriteLine(">");
                        else
                        {
                            int actual = 1;
                            while (xmlReader.MoveToNextAttribute())
                            {
                                string content = String.Format("{0} {1}={2}", attributeIndent, xmlReader.Name, xmlReader.Value);
                                if (actual != xmlReader.AttributeCount)
                                    textWriter.WriteLine(content);
                                else
                                    textWriter.Write(content);

                                if (string.IsNullOrEmpty(attributeIndent))
                                    attributeIndent = indent + Enumerable.Repeat<string>(" ", element.Length + 1).Aggregate((a, b) => a + b);

                                actual++;
                            }
                            xmlReader.MoveToElement();
                            textWriter.WriteLine(">");
                            attributeIndent = "";
                        }
                        if (!xmlReader.IsEmptyElement)
                        {
                            indent += "  ";
                            textWriter.WriteLine(">");
                        }
                        else
                            textWriter.WriteLine("/>");
                    }
                    else if (xmlReader.NodeType == XmlNodeType.EndElement)
                    {
                        indent = indent.Substring(0, indent.Length - 2);
                        textWriter.WriteLine("{0}</{1}>", indent, xmlReader.Name);
                    }
                    else if (xmlReader.NodeType == XmlNodeType.Text)
                    {
                        textWriter.WriteLine(xmlReader.Value);
                    }
                }

                return textWriter.ToString();
            }
        }
    }
于 2010-11-22T20:24:53.427 回答
0

我认为引号是无效的 xml,这可能会给读者/作者带来一些胃痛。除此之外,你为什么不告诉你的差异应用程序忽略空格?

于 2010-12-31T03:54:05.063 回答