Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:39:25+00:00 2026-06-02T17:39:25+00:00

I have a dictionary class that implements IXmlSerializable with custom WriteXml and ReadXml implementation.

  • 0

I have a dictionary class that implements IXmlSerializable with custom WriteXml and ReadXml implementation. (Full code below) When I serialize it using DataContractSerializer, the output contains namespaces for general known types such as int, long, string etc. As shown:

<?xml version="1.0" encoding="utf-16"?>
<MyDictionary>
  <DictionaryItem Key="myInteger">
    <Value>
      <int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">4</int>
    </Value>
  </DictionaryItem>
  <DictionaryItem Key="myLong">
    <Value>
      <long xmlns="http://schemas.microsoft.com/2003/10/Serialization/">5</long>
    </Value>
  </DictionaryItem>
  <DictionaryItem Key="myString">
    <Value>
      <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">testing</string>
    </Value>
  </DictionaryItem>
</MyDictionary>

How can I remove it so that it looks like the below? Thanks!

<?xml version="1.0" encoding="utf-16"?>
<MyDictionary>
  <DictionaryItem Key="myInteger">
    <Value>
      <int>4</int>
    </Value>
  </DictionaryItem>
  <DictionaryItem Key="myLong">
    <Value>
      <long>5</long>
    </Value>
  </DictionaryItem>
  <DictionaryItem Key="myString">
    <Value>
      <string>testing</string>
    </Value>
  </DictionaryItem>
</MyDictionary>

The test code I used to create the output:

private void button1_Click(object sender, EventArgs e)
    {
        MyDictionary myDictionary = new MyDictionary();
        myDictionary.Add("myInteger", 4);
        Int64 myLong = 5;
        myDictionary.Add("myLong", myLong);
        myDictionary.Add("myString", "testing");

        string outputXml = GetXmlForObject(myDictionary);
        System.IO.File.WriteAllText(@"C:\outputXml.txt", outputXml);
    }

    public static string GetXmlForObject(Object obj)
    {
        try
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
            StringBuilder builder = new StringBuilder();
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
            xmlWriterSettings.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(builder, xmlWriterSettings))
            {
                serializer.WriteObject(writer, obj);
            }
            return builder.ToString();
        }
        catch (Exception exception)
        {
            return String.Empty;
        }
    }

Full code below:

[XmlRoot(Namespace = "")]
public class MyDictionary : Dictionary<String, Object>, IXmlSerializable, ISerializable
{
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException("The method is not implemented.");
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        try
        {
            string key = null;
            object value = null;

            if (reader.IsStartElement() && !reader.IsEmptyElement)
            {
                reader.ReadStartElement();

                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    key = reader.GetAttribute("Key");
                    string typeAttribute = reader.GetAttribute("Null");
                    reader.ReadStartElement("DictionaryItem");
                    if (reader.IsEmptyElement)
                    {
                        this.Add(key, null);
                        reader.ReadStartElement();
                    }
                    else
                    {
                        reader.ReadStartElement("Value");
                        value = GetValueDeserializer(reader.LocalName).ReadObject(reader.ReadSubtree());
                        this.Add(key, value);
                        reader.Skip();  // skipping in case the string is empty or null
                        reader.ReadEndElement();  // Reading end node for Value
                    }

                    reader.ReadEndElement();  // Reading end node for element in the list
                }

                reader.ReadEndElement();
            }
        }

        catch (XmlException e)
        {
            throw new XmlException("ReadXml(XmlReader) reader pointing to invalid element", e);
        }
    }

    private DataContractSerializer GetValueDeserializer(string typeStr)
    {
        Type valueType = GetTypeFromStratusType(typeStr);
        return new DataContractSerializer(valueType);
    }

    public static Type GetTypeFromStratusType(string typeStr)
    {
        Type valueType;

        switch (typeStr)
        {
            case "short":
                valueType = typeof(Int16);
                break;
            case "int":
                valueType = typeof(Int32);
                break;
            case "long":
                valueType = typeof(Int64);
                break;
            case "string":
                valueType = typeof(String);
                break;
            default:
                valueType = typeof(String);
                break;
        }

        return valueType;
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        foreach (KeyValuePair<String, Object> item in this)
        {
            writer.WriteStartElement("DictionaryItem");
            writer.WriteAttributeString("Key", item.Key.ToString());
            if (item.Value != null)
            {
                writer.WriteStartElement("Value");
                DataContractSerializer serializer = new DataContractSerializer(item.Value.GetType());
                serializer.WriteObject(writer, item.Value);
            }
            else
            {
                writer.WriteStartElement("Value");
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:39:26+00:00Added an answer on June 2, 2026 at 5:39 pm

    You can create an XmlTextWriter that bypasses writing the namespace, and plug it into your serializer. By default. the serializer uses a text writer that it gets with the creation of the method XmlDictionaryWriter.CreateTextWriter(…), which does write out namespaces (which is, of course, your problem here!)

    This custom serializer would not write out namespaces at all, and it should fix your problem.

    public class CustomXmlTextWriter : XmlTextWriter
    {    
      public override void WriteStartElement(string prefix, string localName, string ns)
      {
        base.WriteStartElement(null, localName, "");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I want to have opportunities to serialize/deserialize. It implements
I have a class that inherits from a generic dictionary as follows: Class myClass
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a class that holds a dictionary of parameters: public class Parameter :
I have data that has been stored using binary serialization for the following class:
I have a ViewModel class that implements the IDataErrorInfo Interface . In each property's
I have the following method in a base class that implements System.ComponentModel.INotifyPropertyChanged: protected virtual
I have a class that inherits a generic dictionary and an inteface public class
Animals is a dictionary class that contains Animal class objects. DictionaryBase implements IDictionaryEnumerator.GetEnumerator() and
I have written the following class which implements(or tries to!) a dictionary with notifications:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.