Shazter
Shazter

Reputation: 171

XDocument.Parse(xmlstring) Space gets lost

Background

I use a Dictionary as lookuptable and with some help from stackoverflow I was able to serialze the object. I use XDocument.Parse to Convert the xmlstring to a XDocument and save the file to harddisk.

Problem

The dictionary has a key with space " " and the DataContractSerializer works as expected, but the XDocument removes the space " ".

Code with Comments

namespace TestIssue001
{
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;

using System.Xml;
using System.Xml.Linq;


[DataContract]
class RemoteData
{
    [DataMember]
    public Dictionary<string, string> Dictionary { get; set; }
}

class Program
{
    public class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding => Encoding.UTF8;
    }

    public static string GenerateXmlResponsewithnested(RemoteData remotedata)
    {
        var xml = "";

        var serializer = new DataContractSerializer(typeof(RemoteData));
        using (var sw = new Utf8StringWriter())
        {
            using (var writer = new XmlTextWriter(sw))
            {
                writer.Formatting = Formatting.Indented; // indent the Xml so it's human readable
                serializer.WriteObject(writer, remotedata);
                writer.Flush();
                xml = sw.ToString();
            }
        }
        return xml;
    }


    static void Main(string[] args)
    {

        RemoteData remoteData = new RemoteData();
        remoteData.Dictionary = new Dictionary<string, string> { { " ", "Whitespace" } };
        // The xml string contains the space character ' ' as key 
        string xmlstring = GenerateXmlResponsewithnested(remoteData);

        // In the doc the space character ' ' as key i removed 
        XDocument doc = XDocument.Parse(xmlstring);


        doc.Save(@".\myconfig.xml");

    }
}
}

Variable xmlstring

<RemoteData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TestIssue001">
  <Dictionary xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfstringstring>
      <d2p1:Key> </d2p1:Key>
      <d2p1:Value>Whitespace</d2p1:Value>
    </d2p1:KeyValueOfstringstring>
  </Dictionary>
</RemoteData>

Variable doc

<RemoteData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TestIssue001">
  <Dictionary xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfstringstring>
      <d2p1:Key></d2p1:Key>
      <d2p1:Value>Whitespace</d2p1:Value>
    </d2p1:KeyValueOfstringstring>
  </Dictionary>
</RemoteData>

Question

How I inform XDocument to keep the space character as key?

Upvotes: 0

Views: 261

Answers (1)

bommelding
bommelding

Reputation: 3037

How I inform XDocument to keep the space character as key?

 XDocument doc = XDocument.Parse(xmlstring, LoadOptions.PreserveWhitespace);

Upvotes: 4

Related Questions