XML indentation, but leaving everything else untouched

My first question is here, so bear with me. Basically, my problem is this: I am creating an XML IDE for the internal language. Its feature should be automatic XML indentation with some command. Like in Visual Studio, etc.

Basically I need to include the following Xml:

<?xml version="1.0" encoding="UTF-8"?>
<note>
        <to>Tove</to>

    <from>Jani</from>
        <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>

</note>

IN:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>

    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>

</note>

This is the indent - but do not touch anything. Is it possible in C # without writing an algorithm from scratch, i.e. with LINQ XDocument or with some XmlWriter implementations?

I have tried the following so far (from What is the easiest way to get deferred XML with line breaks from XmlDocument? )

static public string Beautify(this XmlDocument doc)
{
    StringBuilder sb = new StringBuilder();
    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true,
        IndentChars = "  ",
        NewLineChars = "\r\n",
        NewLineHandling = NewLineHandling.Replace
    };
    using (XmlWriter writer = XmlWriter.Create(sb, settings)) {
        doc.Save(writer);
    }
    return sb.ToString(); 
}

But this removes line breaks and gives me:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

Thanks in advance to anyone who has comments or answers.

+4
3

(, <newline></newline>, Beautify, .

UPDATE: , \n\n "\n", .

+4

Mark, XML- ( ):

class Program
{
    static void Main(string[] args)
    {
        string test = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<note>
    <to>Tove</to>

<from>Jani</from>
    <heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

</note>";
        string output = Test.BeautifyXML(test);
        Console.Write(output);
        Console.ReadLine();
    }
}
static class Test { 
    static public string BeautifyXML(this string docString)
    {
        docString = Regex.Replace(docString.Replace("\r", "<r></r>").Replace("\n", "<n></n>"),@"\?>(<r></r><n></n>)*", "?>");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(docString);
        StringBuilder sb = new StringBuilder();
        XmlWriterSettings settings = new XmlWriterSettings
        {
            Indent = true,
            IndentChars = "  ",
            NewLineChars = "\r\n",
            NewLineHandling = NewLineHandling.Replace
        };
        using (XmlWriter writer = XmlWriter.Create(sb, settings))
        {
            doc.Save(writer);
        }
        return Regex.Replace(sb.ToString().Replace("\r\n", ""), @"<r></r>( )*<n></n>", "\r\n").Replace("?>", "?>\r\n");
    }
}

:

<?xml version="1.0" encoding="utf-16"?>
<note>
  <to>Tove</to>

  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>

</note>
+2

NewLineHandling = NewLineHandling.Replace

NewLineHandling = NewLineHandling.None

None XmlWriter . , .

0

Source: https://habr.com/ru/post/1624027/


All Articles