Solved

An XML and File import/export question with C#

Posted on 2013-05-28
7
867 Views
Last Modified: 2013-05-29
Hi experts
I imported an xml file and save it to another xml file by LINQ in C#.
_xmlDocument = XDocument.Load("input.xml", LoadOptions.PreserveWhitespace);
_xmlDocument.Save("output.xml", SaveOptions.DisableFormatting);

Open in new window


I then opened the files and print out the content byte-by-byte.
using (FileStream fs = File.Open(pathAndFileName, FileMode.Open))
{
                int size = (int)fs.Length; 
                byte[] data = new byte[size]; 
                fs.Read(data, 0, size);
                foreach (byte b in data)
                     Console.WriteLine(b);
}

Open in new window


But when I compared the printed output, the file "output.xml" contains some additional characters: 239, 187, 191,
where 239: Latin small letter i with diaeresis
           187: Right double angle quotes
           191: Inverted question mark

It also dropped 32 (ie Space) that was in "input.xml".

My question is: Is there any way to preserve the format of the input without adding funny characters or discarding space character?

They look identical in a text editor though.

Thanks in advance.
0
Comment
Question by:dominicwong
  • 5
  • 2
7 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39203627
yes, use StreamWriter with Encoding.ASCII:
 using (StreamWriter sw = new StreamWriter(@"output.xml", false, Encoding.ASCII))
           {
               _xmlDocument.Save(sw, SaveOptions.DisableFormatting);
           }

Open in new window

0
 

Author Comment

by:dominicwong
ID: 39203646
Thanks sedgwick for your prompt response.
It resolves the funny character problem. But now it creates one issue:

The original file was:
<?xml version="1.0" encoding="utf-8"?>

Now, the saved file became:
<?xml version="1.0" encoding="us-ascii"?>
0
 

Author Comment

by:dominicwong
ID: 39203650
FYI, the reason they need to be identical is because I need to calculate the CRC of the file later in the program.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39203661
thats true, however if u compare bytes of the two xmls u gonna find they are identical:

List<byte> xml1 = new List<byte>();
           List<byte> xml2 = new List<byte>();
           using (FileStream fs = File.Open(@"input.xml", FileMode.Open))
           {
               int size = (int)fs.Length;
               byte[] data = new byte[size];
               fs.Read(data, 0, size);
               xml1.AddRange(data);
           }
           using (FileStream fs = File.Open(@"output.xml", FileMode.Open))
           {
               int size = (int)fs.Length;
               byte[] data = new byte[size];
               fs.Read(data, 0, size);
               xml2.AddRange(data);
           }

           var countDiffBytes = xml1.Except(xml2).Count();

Open in new window

countDiffBytes is equal to 0, meaning they will pass crc check.
0
 

Author Comment

by:dominicwong
ID: 39203678
Sorry for the confusion. The later CRC calculation hasn't been included in the code (for clarity reason).
The actual code when it comes to calculating CRC is as follows.
Therefore, I do need them to be completely identical; otherwise, the CRC will be different.

            Crc32 crc32 = new Crc32(); 
            String hash = String.Empty;
            using (FileStream fs = File.Open(pathAndFileName, FileMode.Open))
            {
                foreach (byte b in crc32.ComputeHash(fs))
                    hash += b.ToString("x2").ToLower();
            }

Open in new window

0
 

Author Comment

by:dominicwong
ID: 39203725
I managed to get the software requirement to change from "utf-8" to "us-ascii".
Now, it is OK. The problem is resolved.

Thanks for your help.
0
 

Author Closing Comment

by:dominicwong
ID: 39203727
Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now