0

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.

Problembär
  • 171
  • 1
  • 1
  • 9

5 Answers5

1

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).

Christopher B. Adkins
  • 3,499
  • 2
  • 26
  • 29
  • This won't work since the string I write as xml is only one entry in a larger xml file. – Problembär Sep 06 '10 at 08:08
  • 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. – Christopher B. Adkins Sep 06 '10 at 12:53
1

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

Johann Blais
  • 9,389
  • 6
  • 45
  • 65
0

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.

Problembär
  • 171
  • 1
  • 1
  • 9
0

try removing

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

lines, and see what happens.

Numenor
  • 1,686
  • 12
  • 20
  • 2
    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 Sep 06 '10 at 12:23
0

We did overcome this problem by using short tags of the form...

<value />

...instead of...

<value></value>

...then you can't have line breaks in the XML output. To do this, you have to query your tree before passing it to the writer.

/// <summary>
/// Prepares the XML Tree, to write short format tags, instead
/// of an opening and a closing tag. This leads to shorter and
/// particularly to valid XML files.
/// </summary>
protected static void Sto_XmlShortTags(XmlNode node)
{
  // if there are children, make a recursive call
  if (node.ChildNodes.Count > 0)
  {
    foreach (XmlNode childNode in node.ChildNodes)
      Sto_XmlShortTags(childNode);
  }
  // if the node has no children, use the short format
  else
  {
    if (node is XmlElement)
      ((XmlElement)node).IsEmpty = true;
  }
}
martinstoeckli
  • 23,430
  • 6
  • 56
  • 87