Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

An XML and File import/export question with C#

Posted on 2013-05-28
7
871 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

790 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