We help IT Professionals succeed at work.
Get Started

problem when generating StreamWriter with xml

phenryll asked
Last Modified: 2013-12-17
Hi Experts,

I have been writing some code to update attributes' value in a XML file.

I use the XDocument's load method to load the file ; I wanted update some values in the file, but the final application (a mix software called Virtual DJ) that read the XML doesn't read it at all (unfortunately, there is no error message).

One thing leading to another, I wanted just load the file and save it without updating anything (I thought that the operation might have screwed up all the nodes)

XDocument db = XDocument.Load(DbFilePath);
db.Save(TARGET_XML, SaveOptions.None);

Pretty simple.

If I specify that no formatting must be applied, the result is still the same:

db.Save(TARGET_XML, SaveOptions.DisableFormatting);

The problem is still the same : the newly generated file can't be read by the application. I insist on it: nothing is different between those 2 files, visualy speaking

There is something more strange : if I copy a node from the generated file to the original XML file (the file that can be read by the final application), the new node can't be read  and the piece of information that is related to that node isn't correclty displayed in the application.

Trust me, the pasted node is EXACTLY the same (visualy speaking I mean)

If I only copy and paste some attribute's value, there is no problem. Once I copy an expression starting with "<" and leading with ">", it sucks.

I use a different method to generate the file : I supposedly think taht CR and LF may be wrongly written.

I use a StreamWriter object :

        private static void WriteInFile(string path, string stream)
            using (StreamWriter swriter = File.AppendText(path))

As a conclusion, neither XDocument.Save() nor a StreamWriter has solved the issue.

Frankly, I don't know what's wrong.

I tested each char from the newly XML generated file, there is no "bad" character:

                if ((char == 0x9) ||
            (char == 0xA) ||
            (char == 0xD) ||
            ((char >= 0x20) && (char <= 0xD7FF)) ||
            ((char >= 0xE000) && (char <= 0xFFFD))

Also, I check out the file's permission of the the newly genrated xml : permissions are the same that the riginal one.

Do you have any explanation? What do you recommand to do?

Thanks for reading the message.

Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE