开发者

Empty string serialization

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. T开发者_运维技巧he 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.


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


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


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.


try removing

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

lines, and see what happens.


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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜