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;
}
}
}
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>
Related
I'm logging xml object by serializing the object, I've looked at several presented solutions from here and the result is that for sub elements I still get the xmlns=. This caused an issue with the vendor I'm working with as they claimed this was the reason for a failure i was experiencing (it was not). But for future consideration I want to log the xml data we are sending exactly as the Connected service does, without the xmlns=.
This is how the Object is defined (WSDL auto gened code)
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://stamps.com/xml/namespace/2021/01/swsim/SwsimV111")]
public partial class Address : object, System.ComponentModel.INotifyPropertyChanged
{
private string fullNameField;
public Address()
{
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public string FullName
{
get
{
return this.fullNameField;
}
set
{
this.fullNameField = value;
this.RaisePropertyChanged("FullName");
}
}
}
This is the routine I'm using to serialize:
public static string SerializeObject<T>(T dataObject)
{
if (dataObject == null) return string.Empty;
XmlWriterSettings settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
NewLineHandling = NewLineHandling.None,
NewLineOnAttributes = false,
};
XmlAttributes xmlAttributes = new XmlAttributes { Xmlns = false };
var xmlAttributeOverrides = new XmlAttributeOverrides();
xmlAttributeOverrides.Add(dataObject.GetType(), xmlAttributes);
try
{
XmlSerializerNamespaces emptyNS = new XmlSerializerNamespaces(new[] { new XmlQualifiedName(string.Empty, string.Empty) });
using (StringWriter xmlWriter = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(xmlWriter, settings))
{
var serializer = new XmlSerializer(typeof(T), xmlAttributeOverrides);
serializer.Serialize(xmlWriter, dataObject, emptyNS);
return xmlWriter.ToString();
}
}
}
catch (Exception)
{
return string.Empty;
}
}
}
And finally this is the output:
<?xml version="1.0" encoding="utf-16"?>
<CleanseAddressRequest>
<Authenticator>Removed</Authenticator>
<Address>
<FullName xmlns="http://stamps.com/xml/namespace/2021/01/swsim/SwsimV111">Test 103</FullName>
<PhoneNumber xmlns="http://stamps.com/xml/namespace/2021/01/swsim/SwsimV111" />
<FullAddress xmlns="http://stamps.com/xml/namespace/2021/01/swsim/SwsimV111">
103 Linda Lane
Media PA 19063</FullAddress>
</Address>
</CleanseAddressRequest>
Based on my efforts with the vendor I want to be able to use output like:
<?xml version="1.0" encoding="utf-16"?>
<CleanseAddressRequest>
<Authenticator>Removed</Authenticator>
<Address>
<FullName>Test 103</FullName>
<PhoneNumber />
<FullAddress>
103 Linda Lane
Media PA 19063</FullAddress>
</Address>
</CleanseAddressRequest>
The work around I used to resolve my issue was to manually edit the XML, not what is useful for future maintainers of the code base.
How can I programmatically remove the extraneous xmlns=?
Edited for spelling error.
I have a class
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.3081.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://test/v1")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://test/v1", IsNullable=false)]
public partial class Data
{
...
public object Comment { get; set; }
...
}
The Comment property is of type object because it is declared as type any in the xml schema. It's declared as any to allow both text and xhtml data. I can't change the schema - it's related to an international standard.
Single-line content (string):
<Comment>This is a single line text</Comment>
Multi-line content (xhtml):
<Comment>
<div xmlns="http://www.w3.org/1999/xhtml">This is text<br />with line breaks<br />multiple times<div>
</Comment>
The XmlSerializer won't allow me to plug an XmlElement into the object Comment property of the auto-generated Data class. I've also tried to create a custom IXmlSerializer implementation for XHtml, but then the XSD-generated Comment property needs to be declared as that exact type (instead of object).
The custom XHtml type I'm trying to set on the Comment property looks like this;
[XmlRoot]
public class XHtmlText : IXmlSerializable
{
[XmlIgnore]
public string Content { get; set; }
public XmlSchema GetSchema() => null;
public void ReadXml(XmlReader reader) { } // Only used for serializing to XML
public void WriteXml(XmlWriter writer)
{
if (Content.IsEmpty()) return;
writer.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
var lines = Content.Split('\n');
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
writer.WriteRaw(line);
if (i < lines.Length - 1) writer.WriteRaw("<br />");
}
writer.WriteFullEndElement();
}
}
Exception from the XmlSerializer:
InvalidOperationException: The type Lib.Xml.XHtmlText may not be used
in this context. To use Lib.Xml.XHtmlText as a parameter, return type,
or member of a class or struct, the parameter, return type, or member
must be declared as type Lib.Xml.XHtmlText (it cannot be object).
Objects of type Lib.Xml.XHtmlText may not be used in un-typed
collections, such as ArrayLists
The serialization code:
var data = new Lib.Xml.Data { Content = "test\ntest\ntest\n" };
var settings = new XmlWriterSettings()
{
NamespaceHandling = NamespaceHandling.OmitDuplicates,
Indent = false,
OmitXmlDeclaration = omitDeclaration,
};
using (var stream = new MemoryStream())
using (var xmlWriter = XmlWriter.Create(stream, settings))
{
var serializer = new XmlSerializer(data.GetType(), new[] { typeof(Lib.Xml.XHtmlText) });
serializer.Serialize(xmlWriter, data);
return stream.ToArray();
}
Looks like I've found the solution.
I can't set the comment to an instance of XmlElement
data.Comment = commentAsXhtmlXmlElement;
but I can (somehow) assign an array of XmlNode
data.Comment = new XmlNode[] { commentAsXhtmlXmlElement };
Found out when I deserialized an inbound xml-instance of the data POCO.
Strange.
I'm having a bit of trouble getting the desired xml serialization that I want. Thanks beforehand for your help.
So, the xml that I am aiming for looks something like this:
<ChangeSet>
<Change class="object" key="foo">
bar
</Change>
<Change class="testing" key="temp">
temp
</Change>
</ChangeSet>
What I'm actually getting is:
<ChangeSet>
<Change class="object" key="foo">
<Value> bar </Value>
</Change>
<Change class="testing" key="temp">
<Value> temp </Value>
</Change>
</ChangeSet>
Note that the value inside of the Value tags need to be able to be any object. (Collection, object, generic type... etc)
How can I get rid of the Value tags?
C# code:
[Serializable]
[XmlRoot("ChangeSet")]
public class ChangeSet
{
[XmlElement("Change", typeof(Change))]
public List<Change> Changes;
}
public class Change
{
[XmlAttribute("Class")]
public string Class;
[XmlAttribute("Description")]
public string Key;
public object Value;
}
StringBuilder xml = new StringBuilder();
XmlSerializer serializer = new XmlSerializer(objToSerialize.GetType());
XmlWriterSettings settings = new XmlWriterSettings()
{
OmitXmlDeclaration = true,
Indent = true
};
using (StringWriter writer = new StringWriter(xml))
{
using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Removes the xsd & xsi namespace declarations from the xml
serializer.Serialize(xmlWriter, objToSerialize, ns);
}
}
Use [XmlText] attribute over Value of type string
[XMLText]
public string Value;
or use another string property and ignore the Value property
[XMLIgnore]
public object Value;
[XMLText]
public string ValueString
{
get{ return this.Value.ToString(); }
}
How do I serialize an XML-serializable object to an XML fragment (no XML declaration nor namespace references in the root element)?
Here is a hack-ish way to do it without having to load the entire output string into an XmlDocument:
using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
public class Example
{
public String Name { get; set; }
static void Main()
{
Example example = new Example { Name = "Foo" };
XmlSerializer serializer = new XmlSerializer(typeof(Example));
XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add(String.Empty, String.Empty);
StringBuilder output = new StringBuilder();
XmlWriter writer = XmlWriter.Create(output,
new XmlWriterSettings { OmitXmlDeclaration = true });
serializer.Serialize(writer, example, emptyNamespace);
Console.WriteLine(output.ToString());
}
}
You should be able to just serialize like you usually do, and then use the Root property from the resulting document.
You may need to clear the attributes of the element first.
By the way this is awesome.
I implemented this code to make it easy to work with xml fragments as classes quickly and then you can just replace the node when finished. This makes the transition between code and xml ultra-easy.
First create some extension methods.
public static class SerializableFragmentExtensions
{
public static XElement ToElement(this ISerializableFragment iSerializableFragment)
{
var serializer = new XmlSerializer(iSerializableFragment.GetType());
var emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add(String.Empty, String.Empty);
var output = new StringBuilder();
var writer = XmlWriter.Create(output,
new XmlWriterSettings { OmitXmlDeclaration = true });
serializer.Serialize(writer, iSerializableFragment, emptyNamespace);
return XElement.Parse(output.ToString(), LoadOptions.None);
}
public static T ToObject<T>(this XElement xElement)
{
var serializer = new XmlSerializer(typeof (T));
var reader = xElement.CreateReader();
var obj = (T) serializer.Deserialize(reader);
return obj;
}
}
Next Implement the required interface (marker interface--I know you are not supposed to but I think this is the perfect reason to it.)
public interface ISerializableFragment
{
}
Now all you have to do is decorate any Serializable class, you want to convert to an XElement Fragment, with the interface.
[Serializable]
public class SomeSerializableClass : ISerializableFragment
{
[XmlAttribute]
public string SomeData { get; set; }
}
Finally test the code.
static void Main(string[] args)
{
var someSerializableClassObj = new SomeSerializableClass() {SomeData = "Testing"};
var element = someSerializableClass.ToElement();
var backToSomeSerializableClassObj = element.ToObject<SomeSerializableClass>();
}
Thanks again for this amazingly useful code.
Bit confused on the proper decorators to use, or whatever design might be necessary. When serializing a class which is implementing IXmlSerializable is there a way to include the namespace and its prefix in the XmlRoot element?
Class definition for example.
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
[XmlRoot("Classy", Namespace = XML_NS)]
public class TestClass : IXmlSerializable
{
private const string XML_PREFIX = ""; // default namespace
private const string XML_NS = "www.123.com";
private const string XML_MEMBER_PREFIX = "me";
private const string XML_MEMBER_NS = "member.com";
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlsn {
get {
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add(XML_PREFIX, XML_NS);
xsn.Add(XML_MEMBER_PREFIX, XML_MEMBER_NS);
return xsn;
}
}
void IXmlSerializable.WriteXml(XmlWriter writer) {
writer.WriteElementString(XML_MEMBER_PREFIX, "Member1.5",
XML_MEMBER_NS, Member1);
writer.WriteElementString(XML_MEMBER_PREFIX, "Member2.5",
XML_MEMBER_NS, Member2.ToString());
writer.WriteElementString(XML_PREFIX, "Member3.5", XML_NS, Member3);
}
//[XmlElement(ElementName = "Member1.5", Namespace = XML_MEMBER_NS)]
public string Member1 {
get { return "init"; }
set { ; }
}
//[XmlElement(ElementName = "Member2.5", Namespace = XML_MEMBER_NS)]
public int Member2 {
get { return 3; }
set { ; }
}
//[XmlElement(ElementName = "Member3.5", Namespace = XML_NS)]
public string Member3 {
get { return "default namespace"; }
set { ; }
}
// ignore ReadXml/GetSchema
XmlSchema IXmlSerializable.GetSchema() { return null; }
void IXmlSerializable.ReadXml(XmlReader reader) { return; }
}
When serialized via:
TestClass tc = new TestClass();
XmlSerializer ser = new XmlSerializer(typeof(TestClass));
ser(writer, tc, tc.xmlsn); // just a plain XmlWriter to Console.Out
the output is
<?xml version="1.0" encoding="utf-8"?>
<Classy xmlns="www.123.com">
<me:Member1.5 xmlns:me="member.com">init</me:Member1.5>
<me:Member2.5 xmlns:me="member.com">3</me:Member2.5>
<Member3.5>default namespace</Member3.5>
</Classy>
The output I was expecting was:
<?xml version="1.0" encoding="utf-8"?>
<Classy xmlns:me="member.com" xmlns="www.123.com">
<me:Member1.5>init</me:Member1.5>
<me:Member2.5>3</me:Member2.5>
<Member3.5>default namespace</Member3.5>
</Classy>
The second is the output of XmlSerializer if the class's implementation of IXmlSerializable is removed and you uncomment the XmlElement decorators. I know both of the Xml documents returned are valid, but it would be nice to try and remove the redundant namespace declarations. I would assume such a thing is possible, since IXmlSerializable is meant to give greater control over how your class is serialized/deserialized.
Update: A very interesting answer suggested modifying the XmlWriterSettings! It looked like this would clear everything up by modifying the NamespaceHandling property. However this still results in the namespaces being rewritten for each member. Full serialization code below:
XmlSerializer ser = new XmlSerializer(typeof(TestClass));
TestClass tc = new TestClass();
XmlWriterSettings xmlSet = new XmlWriterSettings();
xmlSet.Encoding = System.Text.Encoding.UTF8;
xmlSet.Indent = true;
xmlSet.NamespaceHandling = NamespaceHandling.OmitDuplicates;
XmlWriter writer = XmlWriter.Create(Console.Out, xmlSet);
ser.Serialize(writer, tc); // with/without tc.xmlsn same result
writer.Flush();
writer.Close();
I simply added an String Attribute to WriteXml. It worked!
void IXmlSerializable.WriteXml(XmlWriter writer) {
writer.WriteAttributeString("xmlns:me", "member.com");
writer.WriteAttributeString("xmlns", "www.123.com");
writer.WriteElementString(XML_MEMBER_PREFIX, "Member1.5",
XML_MEMBER_NS, Member1);
writer.WriteElementString(XML_MEMBER_PREFIX, "Member2.5",
XML_MEMBER_NS, Member2.ToString());
writer.WriteElementString(XML_PREFIX, "Member3.5", XML_NS, Member3);
}
and remove [XmlRoot("Classy", Namespace = XML_NS)] and the xmlsn property from the class.
Using the WriteAttributeString as suggested is not completely correct. Here is an example on how to use it correctly:
In the WriteXml method at the beginning add something like this:
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
}
You need to set XmlWriterSetting for it to work. see below pls
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx