-1

I have some xml code that i like to have pretty printed (but is not parsable by tools like XmlDocument etc.) in a browser. I currently write the xml code to a file with

File.WriteAllText(filepath, xmlCode);

When i then open the .xml file in file explorer, I get an error that is can't be parsed. No matter if i open it via code or via file explorer.

However when i copy the exact same message into windows text editor and save it as .xml, it is pretty printed regardless of the browser I open it with. This applies to opening it by code and file explorer.

Does c# or editor add some hidden attributes to the file that is not visible to me (but can be manipulated) which could explain this behaviour?

A colleague of mine said it could have something to do with NTFS streams but I know too little about them.

Oliver
  • 117
  • 2
  • 15
  • 1
    Could be the encoding. Does your XML contain non-ASCII-characters? – PMF Jun 03 '22 at 08:55
  • Did you close the file after you wrote to it? - If not, you should try that. Also, take a look at the file permissions. – Xaver Jun 03 '22 at 09:07
  • I'm guessing it's an encoding issue, but the fact that `XmlDocument` can't parse it makes me think it's the original message. Then when you copy/paste it the bad encoding is stripped out – Charlieface Jun 03 '22 at 09:51
  • 1
    Also how is this different from you previous question https://stackoverflow.com/questions/72485948/how-to-only-pretty-print-xml-code-without-parsing-it-in-c – Charlieface Jun 03 '22 at 10:02
  • 1
    So how does a minimal example of `xmlCode` look? Which parse error do you get exactly? If it is XML then an XML parser used by XmlDocument can parse it. Anyway, the `File.WriteAllText` uses UTF-8 as the encoding, an encoding any XML parser should support. But of course if your `xmlCode` starts with an XML declaration declaring a different encoding, any attempt to parse the written file as XML can fail. – Martin Honnen Jun 03 '22 at 17:44

1 Answers1

0

Thanks for the responses!

It turned out to be more simple than encoding issues and more of a problem of how it was formatted before getting to my end.

Someone must have done:

message.Replace(" ", string.empty);

Which resulted in the xmlns:i part being put together with the attribute name (I belive this is called differently but I don't know the proper name), as such

<AttributeNamexmlns:i="....

My solution: It still is not parcalable for a XmlDocument or similar for some reason (but that is not necessary for me, as long as it is pretty printed), so my current solution is to open it in a browser (specifically a WebBrowser in Windows Forms, but this should work with a "local" browser too):

First I get rid of the spacing mistake (yes this should be done at an earlier stage in the process, this is just temporary):

var index = msg.IndexOf("xmlns:i");
msg = msg.Insert(index, " "); 

Then write it to a file and open it in my custom browser (which is nothing more than a WebBrowser in a form - with nothing modified):

CustomBrowser cb = new CustomBrowser();
cb.Show();
cb.Navigate(filePath);

This then pretty prints the xml doc and displays it. (Thats all I need for my use case)

Oliver
  • 117
  • 2
  • 15