I am perplexed with the resulting .XML file that is modified from my C#.NET code below.
When I open the new Metabase.XML file with notepad, I find that the tabs have been changed to spaces, and in place of my Carriage returns and line feeds, I see 
 which appear to me to look like hex representations of what the white space characters should be.Code:XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(metabasePath + "MetaBase.xml"); XmlElement virDir = xmlDoc.SelectSingleNode("//*[local-name()='IIsWebVirtualDir'][@Location='/LM/W3SVC/1/ROOT/EnterpriseCRM']") as XmlElement; if (virDir != null) { virDir.SetAttribute("Location", "/My/New/stupid/LocationAttribute/PieceofGarbage"); //not a real VirDir, just for testing) xmlDoc.Save(metabasePath + @"RicksNewXMLDocument.xml"); }
So just to make sure it wasn't something I had done in my code that modified the attribute, I copied the original file back over, then I ran the following, which as you can see, simply opens the original file, sets the XMLDocument.PreserveWhiteSpace property to true, and then saves it right back out.
I got the same results. So, I opened up the before and after files with a Hex editor and noticed that both files have a Byte-Order-Mark of EF BB BF, which I understand to be UTF8. So i closed both of the files and this time I tried using an XMLWriter object, along with an XMLWriterSettings object to specify the encoding:Code:XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(metabasePath + "MetaBase.xml"); xmlDoc.Save(metabasePath + "xmlDoc_Save_MetabaseXML.xml");
I got the same 
 for CR/LF again, as well as, now the file is about 11KB larger, and there are a ton of these everywhere: 	 which appear to be in place of TAB characters, (except it looks like that there are still a few tabs in the document that have been changed to a plain space).Code:XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = new UTF8Encoding(true); // Passing false to UTF8Encoding means, do not emit the BOM. using (XmlWriter w = XmlWriter.Create(metabasePath + "XMLWriter_MetaBase.xml", settings)) { xmlDoc.Save(w); }
This is driving me nuts. I have thought about slitting my wrists, but I only have an electric Norelco.![]()
What am I doing wrong here? I would very much appreciate any help you can give me on this. (For reference, I created this with using Visual Studio 2010 Pro (WinForm App), .NET 3.5, Windows 7 64-bit)
-Rick


What am I doing wrong here? I would very much appreciate any help you can give me on this. (For reference, I created this with using Visual Studio 2010 Pro (WinForm App), .NET 3.5, Windows 7 64-bit)
Reply With Quote



Bookmarks