Solved

How to create XDocument from XmlWriter and DataContractSerializer without all the extra attributes

Posted on 2011-09-06
2
1,290 Views
Last Modified: 2013-12-17
Suppose I have a Thing class:

[DataContract]
    public class Thing
    {
        [DataMember]
        public string Name;

        [DataMember]
        public List<KeyValuePair<string, object>> attributes = new List<KeyValuePair<string,object>>();

        [DataMember]
        public List<KeyValuePair<string, object>> Attributes
        {
            get { return attributes; }
            set { attributes = value; }
        }
    }

Open in new window


And then suppose I serialize an object of the Thing class into a XDocument like this:

using (StringWriter stringWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
                {
                    var serializer = new DataContractSerializer(typeof(Thing));                    
                    serializer.WriteObject(xmlWriter, someThing);             
                    xmlWriter.Flush();    
                    string sss = stringWriter.ToString();
                    XDocument xDoc2 = XDocument.Parse(sss);
                }
            }

Open in new window


All I want to get is something like this:

<Thing>
   <keyValuePair>
      <key>key1</key>
      <value>val1</value>
   </keyValuePair>
      <key>key2</key>
      <value>val2</value>
   </keyValuePair>
</Thing>

Open in new window


Instead, what I get is this:

<Thing xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WCFSession">
  <Attributes xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
    <d2p1:KeyValuePairOfstringanyType>
      <d2p1:Key>key1</d2p1:Key>
      <d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">obj1</d2p1:Value>
    </d2p1:KeyValuePairOfstringanyType>
    <d2p1:KeyValuePairOfstringanyType>
      <d2p1:Key>key2</d2p1:Key>
      <d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">obj2</d2p1:Value>
    </d2p1:KeyValuePairOfstringanyType>
  </Attributes>
  <Name>myName</Name>
  <attributes xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
    <d2p1:KeyValuePairOfstringanyType>
      <d2p1:Key>key1</d2p1:Key>
      <d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">obj1</d2p1:Value>
    </d2p1:KeyValuePairOfstringanyType>
    <d2p1:KeyValuePairOfstringanyType>
      <d2p1:Key>key2</d2p1:Key>
      <d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">obj2</d2p1:Value>
    </d2p1:KeyValuePairOfstringanyType>
  </attributes>
</Thing>

Is there a way to use the XmlWriter with the DataContractSerializer to get the simple result I showed above instead all the extra attributes and schema information?



0
Comment
Question by:XTO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
ID: 36491768
What if you used a plain XmlSerializer (under the System.Xml.Serialization namespace)?
0
 
LVL 9

Accepted Solution

by:
Grant Spiteri earned 200 total points
ID: 36499844
Here are 2 Generic xml serializer methods

 
/// <summary>
        /// Serializes from string to specified generic type
        /// </summary>
        /// <typeparam name="T">The type passed in</typeparam>
        /// <param name="typeToSerializeTo">The type of object you wish to serialize to, very handy when passing in IObjects as apose to concrete types.</param>
        /// <param name="objectToDeserialize">The object to deserialize.</param>
        /// <returns></returns>
        public static T SerializeFromString<T>(T typeToSerializeTo, string objectToDeserialize)
        {
            using (var sr = new StringReader(objectToDeserialize))
            using (var reader = new XmlTextReader(sr))
            {
                var xs = new XmlSerializer(typeToSerializeTo.GetType());
                return (T) xs.Deserialize(reader);
            }
        }

        public static T DeserializeValidatedXml<T>(string data)
        {
            using (var sr = new StringReader(data))
            using (var reader = new XmlTextReader(sr))
            {
                var xs = new XmlSerializer(typeof (T));
                return (T) xs.Deserialize(reader);
            }
        }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question