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.

2
  • Yes, for me the problem is likely in value.Value. What's the value ? Commented Sep 6, 2010 at 7:03
  • yes, the string in value.Value is always xml. It comes from an xml serializer. This string does not contain any linebreaks. Commented Sep 6, 2010 at 7:05

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

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

1 Comment

No, the written xml has to be indened.
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.

Comments

0

try removing

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

lines, and see what happens.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.