Difference behavious between file written by code and by text editor?

46 views Asked by At

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.

1

There are 1 answers

0
Oliver On

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)