1

I need to remove all of the xmlns attributes from every element. I am using the XmlSerializer class in c#.

Here is a sample of the XML I am getting from serializing an object returned by a webservice.

Code for serialization

public static string ToXML<T>(object obj,string nameSpace)
{
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    var xml = "";

    using (var stringwriter = new StringWriter())
    {
        var serializer = new XmlSerializer(typeof(T));

        serializer.Serialize(stringwriter, obj,ns);

        xml = xml + stringwriter;
    }

    return xml;
}

Calling Code

 var unitInfo = RC.GetUnitInfo(txtUnitNum.Text);
 var x = XML.DocumentExtensions.ToXML<Vehicles>(unitInfo, "");

Result

<Vehicles>
  <Unit xmlns="http://Entities/2006/09">430160</Unit> 
  <VinNumber xmlns="http://Entities/2006/09">1FUJGP9337</VinNumber> 
  <CustName xmlns="http://Entities/2006/09">Ryder : N/A</CustName> 
  <CustCode xmlns="http://Entities/2006/09">4199</CustCode> 
  <NationalAccountFVM xmlns="http://Entities/2006/09">0</NationalAccountFVM> 
  <VehMake xmlns="http://Entities/2006/09">FREIGHTLINER/MERCEDES</VehMake> 
  <VehModel xmlns="http://Entities/2006/09">PX12564ST CASCADIA</VehModel> 
  <VehYear xmlns="http://Entities/2006/09">2012</VehYear> 
  <VehDescrip xmlns="http://Entities/2006/09">TRACTOR</VehDescrip> 
  <InService xmlns="http://Entities/2006/09">10/28/2011 12:00:00 AM</InService> 
  <EngMake xmlns="http://Entities/2006/09">CUMMINS ENGINE CO.</EngMake> 
  <EngModel xmlns="http://Entities/2006/09">ISX'10 14.9 450/1800</EngModel> 
  <EngSize xmlns="http://Entities/2006/09">450</EngSize> 
  <EngSerial xmlns="http://Entities/2006/09">79502</EngSerial> 
  <TransMake xmlns="http://Entities/2006/09">FULLER TRANS., DIV. EATON</TransMake> 
</Vehicles

I need to serialize the object without getting the xmlns attributes.

Vehicle Object

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Entities/2006/09")]
public partial class Vehicles {

    private string unitField;

    private string vinNumberField;

    /// <remarks/>
    public string Unit {
        get {
            return this.unitField;
        }
        set {
            this.unitField = value;
        }
    }

    /// <remarks/>
    public string VinNumber {
        get {
            return this.vinNumberField;
        }
        set {
            this.vinNumberField = value;
        }
    }
}
DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
  • i get an object from a webservice, when i seralize that object, that is the xml i get – DidIReallyWriteThat Mar 17 '15 at 13:09
  • the ns is code that did not work to fix my problem linked from this question http://stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns – DidIReallyWriteThat Mar 17 '15 at 13:11
  • there is no incoming xml, i receive an object which i then call the ToXML method on. – DidIReallyWriteThat Mar 17 '15 at 13:17
  • I flagged this now as a duplicate of http://stackoverflow.com/questions/258960. Your original code was almost correct, just always add and empty Namespace and you won't have any in your generated XML. If it is still unclear, let me know – Bernd Linde Mar 17 '15 at 13:43
  • if only that was true. i tried it, it didnt work. hence my example here, my question, and my linking to it. You think if it worked i would link to it and say `this doesnt work` – DidIReallyWriteThat Mar 17 '15 at 13:51

1 Answers1

2

I modified your ToXML() method below to always correctly exclude a namespace.

The main difference is how the XmlSerializerNamespaces is instantiated.
Generating an empty namespace disables the inserting of namespaces by the XmlWriter into the output XML.
I also change how you build the output XML since string concatenation is more resource intensive than utilizing a StringBuilder in conjuction with a StringWriter.

public static string ToXML<T>(object obj)
{
  StringBuilder outputXml = new StringBuilder();

  using (var stringwriter = new StringWriter(outputXml))
  {
    // Define a blank/empty Namespace that will allow the generated
    // XML to contain no Namespace declarations.
    XmlSerializerNamespaces emptyNS = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });

    var serializer = new XmlSerializer(typeof(T));
    serializer.Serialize(stringwriter, obj, emptyNS);
  }

  return outputXml.ToString();
}

Passing in a sample object, I get the output XML of

<?xml version="1.0" encoding="utf-16"?>
<Vehicles>
  <Unit>430160</Unit>
  <VinNumber>1FUJGP9337</VinNumber>
  <CustName>Ryder : N/A</CustName>
  <CustCode>4199</CustCode>
  <NationalAccountFVM>0</NationalAccountFVM>
  <VehMake>FREIGHTLINER/MERCEDES</VehMake>
  <VehModel>PX12564ST CASCADIA</VehModel>
  <VehYear>2012</VehYear>
  <VehDescrip>TRACTOR</VehDescrip>
  <InService>2011-10-28T00:00:00</InService>
  <EngMake>CUMMINS ENGINE CO.</EngMake>
  <EngModel>ISX'10 14.9 450/1800</EngModel>
  <EngSize>450</EngSize>
  <EngSerial>79502</EngSerial>
  <TransMake>FULLER TRANS., DIV. EATON</TransMake>
</Vehicles>
Bernd Linde
  • 2,098
  • 2
  • 16
  • 22