简体   繁体   English

XML 的缩进没有按预期工作!

[英]Indentation of XMl not working as expected !!

I am trying to create an xml file using a string data(which is an xml).我正在尝试使用字符串数据(这是一个 xml)创建一个 xml 文件。 But the main problem is that the xml that I have created is not properly formatted.但主要问题是我创建的 xml 格式不正确。 I have used XmlWriterSettings to format the xml,but it is not seems to be working.我已经使用 XmlWriterSettings 来格式化 xml,但它似乎不起作用。 Can anyone tell me what is wrong with this code.谁能告诉我这段代码有什么问题。

    string unformattedXml = @"<datas><data1>sampledata1</data1><datas>";

    XmlWriterSettings xmlSettingsWithIndentation = new XmlWriterSettings { Indent = true};
    using (XmlWriter writer = XmlWriter.Create(Console.Out, xmlSettingsWithIndentation))
       {

           writer.WriteRaw(unformattedXml);
       }

Actually when I load this string in an XmlDocument and then saves it as a file , it was formatted.I just wanted to know why it was not working with XmlWriter.实际上,当我在 XmlDocument 中加载此字符串然后将其保存为文件时,它已被格式化。我只是想知道为什么它不能与 XmlWriter 一起使用。

You help would be much appreciated.您的帮助将不胜感激。

Thanks Alex.谢谢亚历克斯。

To ignore white space, try this:要忽略空白,请尝试以下操作:

private static string FormatXml(string unformattedXml) 
{
    //First read the xml, ignoring whitespace.
    var readeroptions = new XmlReaderSettings { IgnoreWhitespace = true };
    var reader = XmlReader.Create(new StringReader(unformattedXml), readeroptions);
   
    //Then write it out with indentation.
    var sb = new StringBuilder();
    var xmlSettingsWithIndentation = new XmlWriterSettings { Indent = true }; 
                  
    using (var writer = XmlWriter.Create(sb, xmlSettingsWithIndentation))
        writer.WriteNode(reader, true);
            
    return sb.ToString();
}

It should work if you use a XmlReader instead of a raw string.如果您使用XmlReader而不是原始字符串,它应该可以工作。

(I expect it is a typo when your last XML element is not closed property, and that by formatting you refer to correct indentation): (当您的最后一个 XML 元素不是 close 属性时,我希望这是一个错字,并且通过格式化您指的是正确的缩进):

class Program
{
    static void Main(string[] args)
    {
        string unformattedXml = @"<datas><data1>sampledata1</data1></datas>";

        var rdr = XmlReader.Create(new StringReader(unformattedXml));

        var sb = new StringBuilder();
        
        var xmlSettingsWithIndentation =  new XmlWriterSettings 
        { 
            Indent = true
        };

        using (var writer = XmlWriter.Create(sb, xmlSettingsWithIndentation))
            writer.WriteNode(rdr, true);
        
        Console.WriteLine(sb);
        Console.ReadKey();
    }
}

It outputs:它输出:

<?xml version="1.0" encoding="utf-16"?>
<datas>
  <data1>sampledata1</data1>
</datas>

Please cf.请参阅。 similar questions:类似问题:
XmlWriter.WriteRaw indentation XmlWriter.WriteRaw 缩进
XML indenting when injecting an XML string into an XmlWriter 将 XML 字符串注入 XmlWriter 时的 XML 缩进

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

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