I have this code:
public static void Main()
{
var test = new TestXML[] {new TestXML() {TestAttribute = "blah"}};
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
var ns = new XmlSerializerNamespaces();
ns.Add("abc", "a/b/c/d/e/f/g");
var serializer = new XmlSerializer(typeof(TestXML[]));
var sw = new StringWriter();
var xw = XmlWriter.Create(Console.Out, settings);
serializer.Serialize(xw,test,ns);
Console.ReadLine();
}
Continued...
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "a/b/c/d/e/f/g",IncludeInSchema = true)]
public class TestXML
{
public string TestAttribute { get; set; }
}
The code outputs:
<ArrayOfTestXML
xmlns:abc="a/b/c/d/e/f/g"><TestXML><abc:TestAttribute>blah</abc:TestAttribute></TestXML></ArrayOfTestXML>
We have a 3rd party dependency that limits our ability to add a collection class to the serialization process. Is it possible to control serialization so that <TestXML> gets the prefix, abc?
What about not to use standard XmlSerializer (which is good for cases where XML itself not have much constraints and used just for serialize/deserialize) and write own Serialization support for your classes?
Related
I have an xml document, where i serialize data dinamically, appending new data if i have a new request. The object properties i serialize are like this
[XmlRoot("LogRecords")]
public class LogRecord
{
public string Message { get; set; }
public DateTime SendTime { get; set; }
public string Sender { get; set; }
public string Recipient { get; set; }
}
Serializing is done in this way :
var stringwriter = new StringWriter();
var serializer = new XmlSerializer(object.GetType());
serializer.Serialize(stringwriter, object);
var smsxmlStr = stringwriter.ToString();
var smsRecordDoc = new XmlDocument();
smsRecordDoc.LoadXml(smsxmlStr);
var smsElement = smsRecordDoc.DocumentElement;
var smsLogFile = new XmlDocument();
smsLogFile.Load("LogRecords.xml");
var serialize = smsLogFile.CreateElement("LogRecord");
serialize.InnerXml = smsElement.InnerXml;
smsLogFile.DocumentElement.AppendChild(serialize);
smsLogFile.Save("LogRecords.xml");
While serializing i use LogFile.CreateElement("LogRecord") and my xml file looks like this :
<LogRecords>
<LogRecord>
<Message>Some messagge</Message>
<SendTime>2017-12-13T22:04:40.1109661+01:00</SendTime>
<Sender>Sender</Sender>
<Recipient>Name</Recipient>
</LogRecord>
<LogRecord>
<Message>Some message too</Message>
<SendTime>2017-12-13T22:05:08.5720173+01:00</SendTime>
<Sender>sender</Sender>
<Recipient>name</Recipient>
</LogRecord>
</LogRecords>
When i try to deserialize like this
XmlSerializer deserializer = new XmlSerializer(typeof(LogRecord));
TextReader reader = new StreamReader("LogRecords.xml");
object obj = deserializer.Deserialize(reader);
LogRecord records = (LogRecord)obj;
reader.Close();
I get null value for each property Message, Sender Recipient and a random value for SendTime, and i know it's because it doesn't recognise the XmlElement LogRecord i added while serializing..
Is there any way to read this xml element so i can take the right property values?
Ps. Sorry if i have messed up the variables, i tried to simplify the code when i added it here and i may have mixed some variables..
Thank you in advance.
You could try to generate POCO classes from XML in Visual Studio as it's described here.
You could serialize/deserialize those POCOs using with simple util methods like:
public static T DeserializeXML<T>(string content)
{
if (content == null)
return default(T);
XmlSerializer xs = new XmlSerializer(typeof(T));
byte[] byteArray = Encoding.ASCII.GetBytes(content);
var contentStream = new MemoryStream(byteArray);
var xml = xs.Deserialize(contentStream);
return (T)xml;
}
public static string SerializeAsXML(object item)
{
if (item == null)
return null;
XmlSerializer xs = new XmlSerializer(item.GetType());
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true }))
{
xs.Serialize(writer, item);
return sw.ToString();
}
}
}
LogRecords probably should be a collection (e.g. an array in this POCO):
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Log
{
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute("LogRecords")]
[System.Xml.Serialization.XmlArrayItemAttribute("LogRecord", IsNullable = false)]
public LogRecord[] LogRecords { get; set; }
}
for the next XML format:
<Log>
<LogRecords>
<LogRecord>
<Message>Some messagge</Message>
<SendTime>2017-12-13T22:04:40.1109661+01:00</SendTime>
<Sender>Sender</Sender>
<Recipient>Name</Recipient>
</LogRecord>
<LogRecord>
<Message>Some message too</Message>
<SendTime>2017-12-13T22:05:08.5720173+01:00</SendTime>
<Sender>sender</Sender>
<Recipient>name</Recipient>
</LogRecord>
</LogRecords>
</Log>
You manually add the root element in the xml. Therefore, you must also manually skip it when reading.
XmlSerializer deserializer = new XmlSerializer(typeof(LogRecord));
using (var xmlReader = XmlReader.Create("LogRecords.xml"))
{
// Skip root element
xmlReader.ReadToFollowing("LogRecord");
LogRecord record = (LogRecord)deserializer.Deserialize(xmlReader);
}
Remove the [XmlRoot("LogRecords")] attribute to make it work.
Of course, you will always get the first element in the xml.
As already suggested in the comments, use the list.
List<LogRecord> logRecords = new List<LogRecord>();
var logRecord = new LogRecord { ... };
// Store each new logRecord to list
logRecords.Add(logRecord);
var serializer = new XmlSerializer(typeof(List<LogRecord>));
// Serialization is done with just a couple lines of code.
using (var fileStream = new FileStream("LogRecords.xml", FileMode.Create))
{
serializer.Serialize(fileStream, logRecords);
}
// As well as deserialization
using (var fileStream = new FileStream("LogRecords.xml", FileMode.Open))
{
logRecords = (List<LogRecord>)serializer.Deserialize(fileStream);
}
Thus become unnecessary manipulation using XmlDocument and fuss with manually adding-skipping the root node.
I am serializing List of objects List<TestObject>
, and XmlSerializer generates <ArrayOfTestObject> attribute, I want rename it or remove it.
Can it be done with creating new class that encapsulated List as field?
[XmlRoot("Container")]
public class TestObject
{
public TestObject() { }
public string Str { get; set; }
}
List<TestObject> tmpList = new List<TestObject>();
TestObject TestObj = new TestObject();
TestObj.Str = "Test";
TestObject TestObj2 = new TestObject();
TestObj2.Str = "xcvxc";
tmpList.Add(TestObj);
tmpList.Add(TestObj2);
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));
using (XmlWriter writer = XmlWriter.Create(#"C:\test.xml", settings))
{
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(writer, tmpList, namespaces);
}
<ArrayOfTestObject>
<TestObject>
<Str>Test</Str>
</TestObject>
<TestObject>
<Str>xcvxc</Str>
</TestObject>
</ArrayOfTestObject>
The most reliable way is to declare an outermost DTO class:
[XmlRoot("myOuterElement")]
public class MyOuterMessage {
[XmlElement("item")]
public List<TestObject> Items {get;set;}
}
and serialize that (i.e. put your list into another object).
You can avoid a wrapper class, but I wouldn't:
class Program
{
static void Main()
{
XmlSerializer ser = new XmlSerializer(typeof(List<Foo>),
new XmlRootAttribute("Flibble"));
List<Foo> foos = new List<Foo> {
new Foo {Bar = "abc"},
new Foo {Bar = "def"}
};
ser.Serialize(Console.Out, foos);
}
}
public class Foo
{
public string Bar { get; set; }
}
The problem with this is that when you use custom attributes you need to be very careful to store and re-use the serializer, otherwise you get lots of dynamic assemblies loaded into memory. This is avoided if you just use the XmlSerializer(Type) constructor, as it caches this internally automatically.
Change the following line from:
XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));
To:
XmlRootAttribute root = new XmlRootAttribute("TestObjects");
XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>), root);
It should work.
You can add an additional parameter to the XmlSerializer constructor to essentially name the root element.
XmlSerializer xsSubmit = new XmlSerializer(typeof(List<DropDownOption>), new XmlRootAttribute("DropDownOptions"));
This would result in the following structure:
<DropDownOptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DropDownOption>
<ID>1</ID>
<Description>foo</Description>
</DropDownOption>
<DropDownOption>
<ID>2</ID>
<Description>bar</Description>
</DropDownOption>
</DropDownOptions>
Create another class like :
[XmlRoot("TestObjects")]
public class TestObjects: List<TestObject>
{
}
Then apply below code while sealizing :
XmlSerializer serializer = new XmlSerializer(typeof(TestObjects));
MemoryStream memStream = new MemoryStream();
serializer.Serialize(memStream, tmpList);
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.
I am serializing List of objects List<TestObject>
, and XmlSerializer generates <ArrayOfTestObject> attribute, I want rename it or remove it.
Can it be done with creating new class that encapsulated List as field?
[XmlRoot("Container")]
public class TestObject
{
public TestObject() { }
public string Str { get; set; }
}
List<TestObject> tmpList = new List<TestObject>();
TestObject TestObj = new TestObject();
TestObj.Str = "Test";
TestObject TestObj2 = new TestObject();
TestObj2.Str = "xcvxc";
tmpList.Add(TestObj);
tmpList.Add(TestObj2);
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));
using (XmlWriter writer = XmlWriter.Create(#"C:\test.xml", settings))
{
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(writer, tmpList, namespaces);
}
<ArrayOfTestObject>
<TestObject>
<Str>Test</Str>
</TestObject>
<TestObject>
<Str>xcvxc</Str>
</TestObject>
</ArrayOfTestObject>
The most reliable way is to declare an outermost DTO class:
[XmlRoot("myOuterElement")]
public class MyOuterMessage {
[XmlElement("item")]
public List<TestObject> Items {get;set;}
}
and serialize that (i.e. put your list into another object).
You can avoid a wrapper class, but I wouldn't:
class Program
{
static void Main()
{
XmlSerializer ser = new XmlSerializer(typeof(List<Foo>),
new XmlRootAttribute("Flibble"));
List<Foo> foos = new List<Foo> {
new Foo {Bar = "abc"},
new Foo {Bar = "def"}
};
ser.Serialize(Console.Out, foos);
}
}
public class Foo
{
public string Bar { get; set; }
}
The problem with this is that when you use custom attributes you need to be very careful to store and re-use the serializer, otherwise you get lots of dynamic assemblies loaded into memory. This is avoided if you just use the XmlSerializer(Type) constructor, as it caches this internally automatically.
Change the following line from:
XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));
To:
XmlRootAttribute root = new XmlRootAttribute("TestObjects");
XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>), root);
It should work.
You can add an additional parameter to the XmlSerializer constructor to essentially name the root element.
XmlSerializer xsSubmit = new XmlSerializer(typeof(List<DropDownOption>), new XmlRootAttribute("DropDownOptions"));
This would result in the following structure:
<DropDownOptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DropDownOption>
<ID>1</ID>
<Description>foo</Description>
</DropDownOption>
<DropDownOption>
<ID>2</ID>
<Description>bar</Description>
</DropDownOption>
</DropDownOptions>
Create another class like :
[XmlRoot("TestObjects")]
public class TestObjects: List<TestObject>
{
}
Then apply below code while sealizing :
XmlSerializer serializer = new XmlSerializer(typeof(TestObjects));
MemoryStream memStream = new MemoryStream();
serializer.Serialize(memStream, tmpList);
using dotnet 2.0. Code to illustrate :
Class1 c1 = new Class1();
c1.SomeInt = 5;
XmlDocument doc = new XmlDocument();
doc.LoadXml("<anode xmlns=\"xyz\" ><id>123</id></anode>");
c1.Any = new XmlElement[1];
c1.Any[0] = (XmlElement)doc.DocumentElement;
XmlSerializer ser = new XmlSerializer(typeof(Class1));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "xyz");
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create(sb, settings);
writer.WriteStartElement("root");
ser.Serialize(writer, c1, ns);
writer.WriteEndElement();
writer.Close();
string str = sb.ToString();
MessageBox.Show(str);
where Class1 is defined like :
[System.Serializable()]
[System.Xml.Serialization.XmlRoot(Namespace="xyz")]
public class Class1
{
private int someInt;
public int SomeInt
{
get { return someInt; }
set { someInt = value; }
}
private System.Xml.XmlElement[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
{
get
{
return this.anyField;
}
set
{
this.anyField = value;
}
}
}
This code displays the string :
<root><Class1 xmlns="xyz"><SomeInt>5</SomeInt><anode xmlns="xyz"><id>123</id></anode></Class1></root>
This is the correct xml, but I'm wondering if this can be simplified.
What I would like is to not have the redundant xmlns="xyz" part in the "anode" element.
i.e. I would like :
<root><Class1 xmlns="xyz"><SomeInt>5</SomeInt><anode><id>123</id></anode></Class1></root>
Is this possible ?
No, I don't believe you can. You could use an aliased namespace as described in this article: Prettification of XML Serialization within Web Services.
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates