1

Am using following code to deserialize an object,

        using (MemoryStream memoryStream = new MemoryStream())
        {
            try
            {
                XmlWriterSettings writerSettings1 = new XmlWriterSettings();
                writerSettings1.CloseOutput = false;
                writerSettings1.Encoding = System.Text.Encoding.UTF8;
                writerSettings1.Indent = false;
                writerSettings1.OmitXmlDeclaration = true;
                XmlWriter writer1 = XmlWriter.Create(memoryStream, writerSettings1);

                XmlSerializer xs1 = new XmlSerializer(obj.GetType(), string.Empty);
                xs1.UnknownAttribute += new XmlAttributeEventHandler(xs1_UnknownAttribute);
                xs1.UnknownElement += new XmlElementEventHandler(xs1_UnknownElement);
                xs1.UnknownNode += new XmlNodeEventHandler(xs1_UnknownNode);
                xs1.UnreferencedObject += new UnreferencedObjectEventHandler(xs1_UnreferencedObject);
                xs1.Serialize(writer1, obj);
                writer1.Close();

            }
            catch (InvalidOperationException)
            {
                return null;
            }
            memoryStream.Position = 0;
            serializeObjectDoc.Load(memoryStream);

            return serializeObjectDoc.DocumentElement;

After this when i check the returning node i get two extra attributes {Attribute, Name="xmlns:xsi", Value="http://www.w3.org/2001/XMLSchema-instance"} object {System.Xml.XmlAttribute} {Attribute, Name="xmlns:xsd", Value="http://www.w3.org/2001/XMLSchema"} object {System.Xml.XmlAttribute}

I want to know how to disable these two attributes

3
  • Why do you want to "disable" those attributes? They shouldn't harm anything. Commented Mar 5, 2010 at 12:20
  • They shouldn is a hypothetical statement. I do not want extra attributes in my element,thats why.BCos i am using the same node in a xml which is read by different component.I want to make sure it gets exactly same inputs as it needs Commented Mar 5, 2010 at 12:29
  • Am rolling back the edited version. Please put comments if u need any editing Commented Mar 5, 2010 at 12:30

1 Answer 1

3

XmlSerializerNamespaces to the rescue; a simple (but complete) example:

using System.Xml.Serialization;
using System;
public class Foo
{
    public string Bar { get; set; }
    static void Main()
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        XmlSerializer ser = new XmlSerializer(typeof(Foo));
        ser.Serialize(Console.Out, new Foo { Bar = "abc" }, ns);
    }
}
Sign up to request clarification or add additional context in comments.

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.