Frage

I have a valid xml stream and want to write this stream to debug output in an easy readable way.

At the moment I get something like this:

<bla><yadda>hello</yadda><yadda>world</yadda></bla>

But what I want is this:

<bla>
    <yadda>hello</yadda>
    <yadda>world</yadda>
</bla>

Is there an easy way to do this?

Here is my code so far:

stream.Position = 0;
byte[] bbb = stream.GetBuffer();
string str = "";
for(int i = 0; i < stream.Length; i++)
{
  byte b = bbb[i];
  str += Convert.ToChar(b).ToString();
}
Debug.WriteLine(str);
War es hilfreich?

Lösung 2

use XDocument and load the steam

XDocument doc= XDocument.Load(stream);

Debug.WriteLine(doc.ToString());

Andere Tipps

this should work

string xml = "<bla><yadda>hello</yadda><yadda>world</yadda></bla>";
XDocument doc = XDocument.Parse(xml);
Console.WriteLine(doc.ToString());

OUTPUT

<bla>
   <yadda>hello</yadda>
   <yadda>world</yadda>
</bla> 

another way is to use System.Xml.Linq.XElement:

var xel = System.Xml.Linq.XElement.Parse("<bla><yadda>hello</yadda><yadda>world</yadda></bla>");
Console.WriteLine(xel);

this works as well:

StreamReader strm = new StreamReader(@"D:\\maoh.xml");

while (strm.EndOfStream == false)
{
     Console.WriteLine(strm.ReadLine());
}

and gives:

enter image description here

You can use Tidy.Net library for this. It allow you to:

  • break-before-br

    indent

    indent-attributes

    indent-spaces

    markup

    punctuation-wrap

    sort-attributes

    split

    tab-size

    vertical-space

    wrap

    wrap-asp

    wrap-attributes

    wrap-jste

    wrap-php

    wrap-script-literals

    wrap-sections

http://tidy.sourceforge.net/docs/quickref.html

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top