views:

82

answers:

6

Hi,

I'm serializing a xml string with the following code

   StringReader newStringReader = new StringReader(value.Value);
            try
            {
               using (var reader = XmlReader.Create(newStringReader))
               {
                  newStringReader = null;
                  writer.WriteNode(reader, false);
               }
            }
            finally
            {
               if (newStringReader != null)
                  newStringReader.Dispose();
            }

but in the written xml file I have

  <property i:type="string">
   <name>Test</name>
    <value>
    </value>
 </property>

but correct would be

 <property i:type="string">
   <name>Test</name>
   <value></value>
 </property>

since the "value" property is an empty string. The way it is serialized not it returns "\r\n ".

Writer:

XmlTextWriter writer = new XmlTextWriter(output); 
try { writer.Formatting = System.Xml.Formatting.Indented; 
writer.Indentation = 2; // Konfiguration sichern 
WriteConfiguration(config, writer); 
} 
finally { writer.Close(); }

What have I done wrong?

UPDATE: I write a xml configuration file. The values can either be ints, bools, etc or other xml strings. These xml strings are written with the code above. It works fine except for emtpy string elements in the xml string.

UPDATE 2: It works if I manually change the xml string I want to write. I replace all

<tag></tag>

by

<tag/>

Unfortunatley it's not really a "proper" solution to modify a string with regexes.

A: 

I imagine it would be in the writer being used. Please show that code too.

Tim Carter
XmlTextWriter writer = new XmlTextWriter(output); try { writer.Formatting = System.Xml.Formatting.Indented; writer.Indentation = 2; // Konfiguration sichern WriteConfiguration(config, writer); } finally { writer.Close(); }
refer to Johann's answer below
Tim Carter
+1  A: 

I have always had far better luck with the formatting of XML documents when I work with them as XDocument types and simply use the Save method. For example xmlDoc.Save("C:\temp\xmlDoc.xml"); Simple as that. Also with this you can save over and over throughout the editing with little performance hit (at least none that I have noticed).

Adkins
This won't work since the string I write as xml is only one entry in a larger xml file.
you can still (through the use of Linq) work with individual elements from the larger document. If however you are only interested in that one element then go with the XElement class. Either way if you are going to be working with and editing XML then I think the Linq to XML is something that would be well worth looking into for you.
Adkins
+1  A: 

I would say this is caused by the indentation parameters. Did you try setting the Indented property to false?

Johann Blais
No, the written xml has to be indened.
A: 

This answer might help

KMan
Hmmm, but I'm writing a whole xml document, not just single elements
A: 

I'm writing an xml file with settings. In this file can also be other xml documents. The code above ensures that it's not written in a single line with > etc but as xml in xml. So I have to work with the WriteNode-Method.

A: 

try removing

writer.Formatting = System.Xml.Formatting.Indented; 
writer.Indentation = 2; 

lines, and see what happens.

Numenor
No, the xml has to be formatted.
ok, but just try it? maybe the implementation of indentation applies the \r\n just check to see it. If its the source of the problem then you have a different question to find answer for.
Numenor