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
Related
I am trying to get my output in a specified format for example
Expected
<?xml version="1.0"?>
<xml>
<JournalEntries>
<JournalEntry>
<Field1>SampleOne</Field1>
<Field2>SampleTwo</Field2>
</JournalEntry>
<JournalEntry>
<Field1>SampleOne</Field1>
<Field2>SampleTwo</Field2>
</JournalEntry>
</JournalEntries>
</xml>
My Current Output:
<?xml version="1.0"?>
<xml>
<JournalEntry>
<Field1>SampleOne</Field1>
<Field2>SampleTwo</Field2>
</JournalEntry>
<JournalEntry>
<Field1>SampleOne</Field1>
<Field2>SampleTwo</Field2>
</JournalEntry>
</xml>
So essentially I need to add another root ? in a sense so that there is one JournalEntries at the start and end. Since a group of entries consists of multiple Entry
[XmlRoot(ElementName = "xml")]
public class JournalDocument
{
public JournalDocument()
{
}
public JournalDocument(UnProcessedDocument input)
{
input.Body.ForEach(o =>
{
JournalEntries.Add(new JournalEntry
{
Field1 = "SampleOne"
Field2 = "SampleTwo"
}); ;
});
}
[XmlElement("JournalEntry")]
public List<JournalEntry> JournalEntries { get; set; } = new List<JournalEntry>();
}
public class JournalEntry
{
public string Field1 {get;set;}
public string Field2 {get;set}
}
I don't know if i have my defined root and elements in the correct place and i've tried moving them around but to no luck.. for example i tried putting [XmlRoot(ElementName = "JournalEntries")] just above my JournalEntry class
I prefer using IXmlSerializable.
It's more flexible to me and gives you much more control over the serialization.
Example:
public class JournalDocument : IXmlSerializable
{
public XmlSchema GetSchema() => null;
public void WriteXml(XmlWriter writer)
{
writer.WriteStartDocument();
writer.WriteStartElement("xml");
writer.WriteStartElement("JournalEntries")
if(this.JournalEntries.Count != 0)
{
foreach(JournalEntrie entrie in this.JournalEntries)
{
writer.WriteStartElement("JournalEntrie");
writer.WriteElementString("Field1", entrie.Field1)
writer.WriteElementString("Field2", entrie.Field2)
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.WriteEndDocument(); // close all open elements
}
public void ReadXml(XmlReader reader)
{
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.element)
{
JournalEntrie entrie;
switch(reader.Name)
{
case "JournalEntrie":
entrie = new JournalEntrie();
break;
case "Field1":
entire.Field1 = reader.ReadElementContentAsString();
break;
// Now make this for every element.
}
}
}
}
}
Although IXMLSerializer was an option I ended up creating an extension method instead
public static string Serialize<T>(this object obj)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
var xmlWriterSettings = new XmlWriterSettings() { Indent = true };
XmlSerializer serializer = new XmlSerializer(typeof(T));
var stringWriter = new StringWriter();
using var writer = XmlWriter.Create(stringWriter, xmlWriterSettings);
serializer.Serialize(writer, obj, ns);
writer.Close();
return stringWriter.ToString();
}
And my main code I could keep the same with [XmlRoot(ElementName = "xml")] but just remove the [XmlElement("JournalEntry")]
This ended up solving my issue.
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>
I have the following c# object:
class Modification {
public string Name;
public string Value;
}
I want to use the serializer to serialize my object the following way:
<name>value</name>
Example: Let's say we set those variables to
Name = "Autoroute"
Value = 53
I want the xml to look like:
<test>
<Autoroute>53</Autoroute>
</test>
I saw somewhere that this feature is not supported by the serializer, but is there a way to overload the serializer to allow this kind of behavior ?
Changing the XML structure is not an option since it is already a convention.
You can use IXmlSerializable to do this, though this doesn't give you control over the root element name - you have to set this in the serializer (which may present other challenges when you come to read it as part of a larger xml structure...).
public class Modification : IXmlSerializable
{
public string Name;
public string Value;
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
reader.ReadStartElement();
Name = reader.Name;
Value = reader.ReadElementContentAsString();
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString(Name, Value);
}
}
Usage,
Modification modification = new Modification()
{
Name = "Autoroute",
Value = "53"
};
Modification andBack = null;
string rootElement = "test";
XmlSerializer s = new XmlSerializer(typeof(Modification), new XmlRootAttribute(rootElement));
using (StreamWriter writer = new StreamWriter(#"c:\temp\output.xml"))
s.Serialize(writer, modification);
using (StreamReader reader = new StreamReader(#"c:\temp\output.xml"))
andBack = s.Deserialize(reader) as Modification;
Console.WriteLine("{0}={1}", andBack.Name, andBack.Value);
The XML produced by this looks like this,
<?xml version="1.0" encoding="utf-8"?>
<test>
<Autoroute>53</Autoroute>
</test>
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.
Can I make XmlSerializer ignore the namespace (xmlns attribute) on deserialization so that it doesn't matter if the attribute is added or not or even if the attribute is bogus? I know that the source will always be trusted so I don't care about the xmlns attribute.
Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.
Define an XmlTextReader that ignores namespaces. Like so:
// helper class to ignore namespaces when de-serializing
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader): base(reader) { }
public override string NamespaceURI
{
get { return ""; }
}
}
// helper class to omit XML decl at start of document when serializing
public class XTWFND : XmlTextWriter {
public XTWFND (System.IO.TextWriter w) : base(w) { Formatting= System.Xml.Formatting.Indented;}
public override void WriteStartDocument () { }
}
Here's an example of how you would de-serialize using that TextReader:
public class MyType1
{
public string Label
{
set { _Label= value; }
get { return _Label; }
}
private int _Epoch;
public int Epoch
{
set { _Epoch= value; }
get { return _Epoch; }
}
}
String RawXml_WithNamespaces = #"
<MyType1 xmlns='urn:booboo-dee-doo'>
<Label>This document has namespaces on its elements</Label>
<Epoch xmlns='urn:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'>0</Epoch>
</MyType1>";
System.IO.StringReader sr;
sr= new System.IO.StringReader(RawXml_WithNamespaces);
var s1 = new XmlSerializer(typeof(MyType1));
var o1= (MyType1) s1.Deserialize(new NamespaceIgnorantXmlTextReader(sr));
System.Console.WriteLine("\n\nDe-serialized, then serialized again:\n");
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("urn", "booboo-dee-doo");
s1.Serialize(new XTWFND(System.Console.Out), o1, ns);
Console.WriteLine("\n\n");
The result is like so:
<MyType1>
<Label>This document has namespaces on its elements</Label>
<Epoch>0</Epoch>
</MyType1>
If you expect no namespace, but the input has namespaces, then you can set
Namespaces = false
on your XmlTextReader.
Exdended Wolfgang Grinfeld answer (w/o exception handling):
public static Message Convert(XmlDocument doc)
{
Message obj;
using (TextReader textReader = new StringReader(doc.OuterXml))
{
using (XmlTextReader reader = new XmlTextReader(textReader))
{
reader.Namespaces = false;
XmlSerializer serializer = new XmlSerializer(typeof(Message));
obj = (Message)serializer.Deserialize(reader);
}
}
return obj;
}
Solved this by using XmlSerializer Deserialize to read from xml instead from stream. This way before xml is Deserialized, using Regex to remove xsi:type from the xml. Was doing this is Portable Class Library for Cross Platform, so did not had many other options :(. After this the deserialization seems to work fine.
Following code can help,
public static TClass Deserialize<TClass>(string xml) where TClass : class, new()
{
var tClass = new TClass();
xml = RemoveTypeTagFromXml(xml);
var xmlSerializer = new XmlSerializer(typeof(TClass));
using (TextReader textReader = new StringReader(xml))
{
tClass = (TClass)xmlSerializer.Deserialize(textReader);
}
return tClass;
}
public static string RemoveTypeTagFromXml(string xml)
{
if (!string.IsNullOrEmpty(xml) && xml.Contains("xsi:type"))
{
xml = Regex.Replace(xml, #"\s+xsi:type=""\w+""", "");
}
return xml;
}
Why try to make the XmlSerializer forget how XML works? It's a fact of XML that two elements with the same name but different namespaces are different elements.
If you want to process XML that has no namespaces, then you should pre-process it to remove the namespaces, and then pass it to the serializer.