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;
        }
    }
}
5
  • i get an object from a webservice, when i seralize that object, that is the xml i get Commented Mar 17, 2015 at 13:09
  • the ns is code that did not work to fix my problem linked from this question stackoverflow.com/questions/258960/… Commented Mar 17, 2015 at 13:11
  • there is no incoming xml, i receive an object which i then call the ToXML method on. Commented Mar 17, 2015 at 13:17
  • I flagged this now as a duplicate of 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 Commented Mar 17, 2015 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 Commented Mar 17, 2015 at 13:51

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

7 Comments

same result with xmlns still in
Then show the definition of the incoming object, since with my testing object, that was the output
i added a small vehicle object
Do you have control over the creation/maintenance of the Vehicles object?
i wish i did...the creation yes, the maintenance no
|

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.