Problems serializing a class to XML and including a CDATA section - c#

I have a class that is serialized into XML for consumption by a web service. In this classes instance the XML must include a CDATA section for the web service to read it but I am at a loss on how to implement this.
The XML needs to look like:
<UpdateOrderStatus>
<Action>2</Action>
<Value>
<![CDATA[
<Shipment>
<Header>
<SellerID>
...
]]>
</Value>
</UpdateOrderStatus>
I am able to generate the appropriate XML, except for the CDATA part.
My class structure looks like:
public class UpdateOrderStatus
{
public int Action { get; set; }
public ValueInfo Value { get; set; }
public UpdateOrderStatus()
{
Value = new ValueInfo();
}
public class ValueInfo
{
public ShipmentInfo Shipment { get; set; }
public ValueInfo()
{
Shipment = new ShipmentInfo();
}
public class ShipmentInfo
{
public PackageListInfo PackageList { get; set; }
public HeaderInfo Header { get; set; }
public ShipmentInfo()
{
PackageList = new PackageListInfo();
Header = new HeaderInfo();
}
....
I have seen some suggestions on using:
[XmlElement("node", typeof(XmlCDataSection))]
but that causes an exception
I have also tried
[XmlElement("Value" + "<![CDATA[")]
but the resulting XML is incorrect showing
<Value_x003C__x0021__x005B_CDATA_x005B_>
....
</Value_x003C__x0021__x005B_CDATA_x005B_>
Can anyone show me what I am doing wrong, or where I need to go with this?
--Edit--
making shipmentInfo serializable per carlosfigueira works for the most part, however I get extra ? characters in the resulting XML ( see post Writing an XML fragment using XmlWriterSettings and XmlSerializer is giving an extra character for details )
As such I changed the Write XML method to:
public void WriteXml(XmlWriter writer)
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false);
settings.Indent = true;
using (XmlWriter innerWriter = XmlWriter.Create(ms, settings))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment,ns);
innerWriter.Flush();
writer.WriteCData(Encoding.UTF8.GetString(ms.ToArray()));
}
}
}
However I am not getting an exception:
System.InvalidOperationException: There was an error generating the XML document. ---> System.ArgumentException: '.', hexadecimal
value 0x00, is an invalid character.
--Edit --
The exception was caused by the inclusion of my previous serializeToString method. Since removing that the CDATA output is correct, except for a spacing issue, but I am also getting a namespace and xml declaration that should be removed by the XML settings specified. Output is:
<?xml version="1.0"?>
<UpdateOrderStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Action>1</Action>
<Value><![CDATA[< S h i p m e n t I n f o >
< P a c k a g e L i s t >
< P a c k a g e >
< S h i p D a t e > 2 0 1 2 - 0 7 - 1 3 T 1 1 : 5 8 : 5 1 . 0 9 2 5 6 1 5 - 0 4 : 0 0 < / S h i p D a t e >
< I t e m L i s t >
< I t e m >
< S h i p p e d Q t y > 0 < / S h i p p e d Q t y >
< / I t e m >
< / I t e m L i s t >
< / P a c k a g e >
< / P a c k a g e L i s t >
< H e a d e r >
< S e l l e r I d > S h i p m e n t h e a d e r < / S e l l e r I d >
< S O N u m b e r > 0 < / S O N u m b e r >
< / H e a d e r >
< / S h i p m e n t I n f o > ]]></Value>
</UpdateOrderStatus>
Any ideas of avoiding the BOM using the new class?
--Edit 3 -- SUCCESS!
I have implemented changes suggested below and now have the following writer class and test methods:
UpdateOrderStatus obj = new UpdateOrderStatus();
obj.Action = 1;
obj.Value = new UpdateOrderStatus.ValueInfo();
obj.Value.Shipment = new UpdateOrderStatus.ValueInfo.ShipmentInfo();
obj.Value.Shipment.Header.SellerId = "Shipment header";
obj.Value.Shipment.PackageList = new UpdateOrderStatus.ValueInfo.ShipmentInfo.PackageListInfo();
obj.Value.Shipment.PackageList.Package = new UpdateOrderStatus.ValueInfo.ShipmentInfo.PackageListInfo.PackageInfo();
obj.Value.Shipment.PackageList.Package.ShipDate = DateTime.Now;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = new UTF8Encoding(false);
settings.Indent = true;
XmlSerializer xs = new XmlSerializer(typeof(UpdateOrderStatus));
MemoryStream ms = new MemoryStream();
XmlWriter writer = XmlWriter.Create(ms, settings);
xs.Serialize(writer, obj, ns);
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
public void WriteXml(XmlWriter writer)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
StringBuilder sb = new StringBuilder();
using (XmlWriter innerWriter = XmlWriter.Create(sb, settings))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment, ns);
innerWriter.Flush();
writer.WriteCData(sb.ToString());
}
}
This produces the following XML:
<UpdateOrderStatus>
<Action>1</Action>
<Value><![CDATA[<ShipmentInfo>
<PackageList>
<Package>
<ShipDate>2012-07-13T14:05:36.6170802-04:00</ShipDate>
<ItemList>
<Item>
<ShippedQty>0</ShippedQty>
</Item>
</ItemList>
</Package>
</PackageList>
<Header>
<SellerId>Shipment header</SellerId>
<SONumber>0</SONumber>
</Header>
</ShipmentInfo>]]></Value>
</UpdateOrderStatus>

In response to the 'spaces' you are seeing after your edit, it is because of the encoding you are using (Unicode, 2 bytes per character).
Try:
settings.Encoding = new Utf8Encoding(false);
EDIT:
Also, note that format of the MemoryStream is not necessarily a valid UTF-8 encoded string! You can use a StringBuilder instead of MemoryStream to create your inner writer.
public void WriteXml(XmlWriter writer)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
StringBuilder sb = new StringBuilder();
using (XmlWriter innerWriter = XmlWriter.Create(sb, settings))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment,ns);
innerWriter.Flush();
writer.WriteCData(sb.ToString());
}
}

Could this be of any help: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx
//Create a CData section.
XmlCDataSection CData;
CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");
//Add the new node to the document.
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
Also, what Exception did you get when using the attribute?
-- edit --
You could try adding a custom class, and do something like this:
some xml serializable class,
{
.......
[XmlElement("PayLoad", Type=typeof(CDATA))]
public CDATA PayLoad
{
get { return _payLoad; }
set { _payLoad = value; }
}
}
public class CDATA : IXmlSerializable
{
private string text;
public CDATA()
{}
public CDATA(string text)
{
this.text = text;
}
public string Text
{
get { return text; }
}
/// <summary>
/// Interface implementation not used here.
/// </summary>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
/// <summary>
/// Interface implementation, which reads the content of the CDATA tag
/// </summary>
void IXmlSerializable.ReadXml(XmlReader reader)
{
this.text = reader.ReadElementString();
}
/// <summary>
/// Interface implementation, which writes the CDATA tag to the xml
/// </summary>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteCData(this.text);
}
}
As found here http://bytes.com/topic/net/answers/530724-cdata-xmltextattribute

Implementing ShipmentInfo as an IXmlSerializable type will get close to what you need - see example below.
public class StackOverflow_11471676
{
public class UpdateOrderStatus
{
public int Action { get; set; }
public ValueInfo Value { get; set; }
}
[XmlType(TypeName = "Shipment")]
public class ShipmentInfo
{
public string Header { get; set; }
public string Body { get; set; }
}
public class ValueInfo : IXmlSerializable
{
public ShipmentInfo Shipment { get; set; }
private XmlSerializer shipmentInfoSerializer = new XmlSerializer(typeof(ShipmentInfo));
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
using (MemoryStream ms = new MemoryStream(
Encoding.UTF8.GetBytes(
reader.ReadContentAsString())))
{
Shipment = (ShipmentInfo)this.shipmentInfoSerializer.Deserialize(ms);
}
}
public void WriteXml(XmlWriter writer)
{
using (MemoryStream ms = new MemoryStream())
{
using (XmlWriter innerWriter = XmlWriter.Create(ms, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
shipmentInfoSerializer.Serialize(innerWriter, this.Shipment);
innerWriter.Flush();
writer.WriteCData(Encoding.UTF8.GetString(ms.ToArray()));
}
}
}
}
public static void Test()
{
UpdateOrderStatus obj = new UpdateOrderStatus
{
Action = 1,
Value = new ValueInfo
{
Shipment = new ShipmentInfo
{
Header = "Shipment header",
Body = "Shipment body"
}
}
};
XmlSerializer xs = new XmlSerializer(typeof(UpdateOrderStatus));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, obj);
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
}

The below example is only when the structure of the schema is defined and you have no choice of altering the schema.
When you Deserialize/Serialize a [xmltext] value it is very difficult to hold the text in the CDATA[]
enclose. you can use compiletransform to get the CDATA value in the xml as it is but the CDATA is lost as soon as you deserialize in C# and load to memory stuff.
This is one of the easiest way to do
deserialize/Serialize
once the final xml output is derived. The final xml can be
converted to string and parsed as shown below and return it as
string will embed CDATA to the test1 value.
string xml ="<test><test1>###!#!#!##!##%$%##$%#$%</test1></test>";
XNamespace ns = #"";
XDocument doc = XDocument.Parse(xml);
string xmlString = string.Empty;
var coll = from query in doc.Descendants(ns + "test1")
select query;
foreach (var value in coll){
value.ReplaceNodes(new XCData(value .Value));
}
doc.save("test.xml");// convert doc.tostring()

I think carlosfigueira just gave an elegant answer, but perhaps a little hard to understand at first sight. Here is an alternative for your consideration, you can Serialize/Deserialize UpdateOrderStatus and ShipmentInfo separately.
Define your business object class as:
[XmlRoot("UpdateOrderStatus")]
public class UpdateOrderStatus
{
[XmlElement("Action")]
public int Action { get; set; }
private String valueField;
[XmlElement("Value")]
public XmlCDataSection Value
{
get
{
XmlDocument xmlDoc = new XmlDocument();
return xmlDoc.CreateCDataSection(valueField);
}
set
{
this.valueField = value.Value;
}
}
[XmlIgnore]
public ShipmentInfo Shipment
{
get;
set;
}
}
[XmlRoot("ShipmentInfo")]
public class ShipmentInfo
{
[XmlElement("Package")]
public String Package { get; set; }
[XmlElement("Header")]
public String Header { get; set; }
}
Note that you should use XmlCDataSection for the Value field.
Here is the test/helper functions:
// Test function
const string t = #"<UpdateOrderStatus>
<Action>2</Action>
<Value>
<![CDATA[<ShipmentInfo>
<Package>packageInfo</Package>
<Header>headerInfo</Header>
</ShipmentInfo>]]>
</Value>
</UpdateOrderStatus>";
static void Test1()
{
UpdateOrderStatus os = Deserialize(t);
String t2 = XmlUtil.Serialize(os);
}
// Helper functions
static UpdateOrderStatus Deserialize(String str)
{
UpdateOrderStatus os = XmlUtil.DeserializeString<UpdateOrderStatus>(str);
os.Shipment = XmlUtil.DeserializeString<ShipmentInfo>(os.Value.InnerText);
return os;
}
public static class XmlUtil
{
public static String Serialize<T>(T o)
{
XmlSerializer s = new XmlSerializer(typeof(T)); //, overrides);
//StringBuilder builder = new StringBuilder();
MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);
s.Serialize(xmlWriter, o, ns);
}
Byte[] bytes = ms.ToArray();
// discard the BOM part
Int32 idx = settings.Encoding.GetPreamble().Length;
return Encoding.UTF8.GetString(bytes, idx, bytes.Length - idx);
}
public static T DeserializeString<T>(String content)
{
using (TextReader reader = new StringReader(content))
{
XmlSerializer s = new XmlSerializer(typeof(T));
return (T)s.Deserialize(reader);
}
}
...
}

Related

XMl Serialize and Deserialize Multiple derived Classes in C# [duplicate]

This question already has answers here:
Using XmlSerializer to serialize derived classes
(3 answers)
Closed 4 years ago.
I tried following the Microsoft website documentation for addressing the above question. However, I'm not able to find the answer. So I tried writing the code, but my object is not getting serialized when I added more derived classes.
Here goes the code:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
public class Orchestra
{
// public Instrument[] Instruments;
public List<Instrument> Instruments;
public int i;
public float f;
public string s1;
public string s2;
public B bc;
}
public class B
{
public double dd;
}
public class Instrument
{
public string Name;
}
public class Brass : Instrument
{
public bool IsValved;
}
public class Percussion : Instrument
{
public string name;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("Override.xml");
test.DeserializeObject("Override.xml");
}
public void SerializeObject(string filename)
{
/* Each overridden field, property, or type requires
an XmlAttributes object. */
XmlAttributes attrs = new XmlAttributes();
/* Create an XmlElementAttribute to override the
field that returns Instrument objects. The overridden field
returns Brass objects instead. */
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
attrs.XmlElements.Add(attr);
// attrs.XmlArrayItems.Add(attr);
attr.ElementName = "Percussion";
attr.Type = typeof(Percussion);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Create the XmlAttributeOverrides object.
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
/* Add the type of the class that contains the overridden
member and the XmlAttributes to override it with to the
XmlAttributeOverrides object. */
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create the object that will be serialized.
Orchestra band = new Orchestra();
// Create an object of the derived type.
//Brass i = new Brass();
//i.Name = "Trumpet";
//i.IsValved = true;
//Instrument[] myInstruments = { i };
//band.Instruments = myInstruments;
List<Instrument> myInstruments = new List<Instrument>();
myInstruments.Add(new Brass() { Name = "Trumpet", IsValved = true });
myInstruments.Add(new Percussion() { Name = "Percussion", name = "Mridangam" });
band.Instruments = myInstruments;
band.i = 128;
band.f = 5.678f;
band.s1 = "Hi!";
band.s2 = "GOOD!!!";
B b = new B();
b.dd = 2.35674848;
band.bc = b;
// Serialize the object.
s.Serialize(writer, band);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Create an XmlElementAttribute to override the Instrument.
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
FileStream fs = new FileStream(filename, FileMode.Open);
Orchestra band = (Orchestra)s.Deserialize(fs);
Console.WriteLine(band.i);
Console.WriteLine(band.f);
Console.WriteLine(band.s1);
Console.WriteLine(band.s2);
Console.WriteLine(band.bc.dd);
Console.WriteLine("Brass:");
/* The difference between deserializing the overridden
XML document and serializing it is this: To read the derived
object values, you must declare an object of the derived type
(Brass), and cast the Instrument instance to it. */
//Brass b;
//Percussion p;
Brass b;
// Percussion p;
//b = (Brass)i;
// int ii = 0;
foreach (Instrument i in band.Instruments)
//foreach(Instrument i in band.List<Instrument>)
{
// int i = 0;
// ii++;
// if (ii == 1)
// {
b = (Brass)i;
Console.WriteLine(
b.Name + "\n" +
b.IsValved);
// }
/*if (ii == 2)
{
p = (Percussion)i;
Console.WriteLine(
p.Name + "\n" +
p.name);
}*/
}
}
}
I even tried using XmlArrayItem. Could anyone guide how to go about this?
You have to add a new class Instruments to your code. See code below :
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Xml;
public class Orchestras
{
public List<Orchestra> orchestras = new List<Orchestra>();
}
public class Orchestra
{
[XmlElement]
public List<Instrument> Instruments { get; set; }
public int i;
public float f;
public string s1;
public string s2;
public B bc;
}
public class B
{
public double dd;
}
[XmlInclude(typeof(Brass))]
[XmlInclude(typeof(Percussion))]
public class Instrument
{
public string Name;
}
public class Brass : Instrument
{
public bool IsValved;
}
public class Percussion : Instrument
{
public string name;
}
public class Run
{
const string FILENAME = #"c:\temp\test.xml";
public static void Main()
{
Run test = new Run();
test.SerializeObject(FILENAME);
test.DeserializeObject(FILENAME);
}
public void SerializeObject(string filename)
{
Orchestras orchastras = new Orchestras();
Orchestra orchestra1 = new Orchestra();
orchastras.orchestras.Add(orchestra1);
List<Instrument> instruments = new List<Instrument>() {
new Instrument() { Name = "Brass"},
new Instrument() { Name = "Percussion"}
};
orchestra1.Instruments = instruments;
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s = new XmlSerializer(typeof(Orchestras));
// Writing the file requires a TextWriter.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(filename,settings);
// Create the object that will be serialized.
Orchestra band = new Orchestra();
orchastras.orchestras.Add(band);
// Create an object of the derived type.
//Brass i = new Brass();
//i.Name = "Trumpet";
//i.IsValved = true;
//Instrument[] myInstruments = { i };
//band.Instruments = myInstruments;
List<Instrument> myInstruments = new List<Instrument>();
myInstruments.Add(new Brass() { Name = "Trumpet", IsValved = true });
myInstruments.Add(new Percussion() { Name = "Percussion", name = "Mridangam" });
band.Instruments = myInstruments;
band.i = 128;
band.f = 5.678f;
band.s1 = "Hi!";
band.s2 = "GOOD!!!";
B b = new B();
b.dd = 2.35674848;
band.bc = b;
// Serialize the object.
s.Serialize(writer, orchastras);
writer.Close();
}
public void DeserializeObject(string filename)
{
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestras));
FileStream fs = new FileStream(filename, FileMode.Open);
Orchestras band = (Orchestras)s.Deserialize(fs);
Console.WriteLine(band.orchestras[1].i);
Console.WriteLine(band.orchestras[1].f);
Console.WriteLine(band.orchestras[1].s1);
Console.WriteLine(band.orchestras[1].s2);
Console.WriteLine(band.orchestras[1].bc.dd);
Console.WriteLine("Brass:");
/* The difference between deserializing the overridden
XML document and serializing it is this: To read the derived
object values, you must declare an object of the derived type
(Brass), and cast the Instrument instance to it. */
//Brass b;
//Percussion p;
Instrument b;
// Percussion p;
//b = (Brass)i;
// int ii = 0;
foreach (Instrument i in band.orchestras[1].Instruments)
//foreach(Instrument i in band.List<Instrument>)
{
// int i = 0;
// ii++;
// if (ii == 1)
// {
b = i;
Console.WriteLine(
b.Name + "\n");
// }
/*if (ii == 2)
{
p = (Percussion)i;
Console.WriteLine(
p.Name + "\n" +
p.name);
}*/
}
}
}

XMLDiff fails to recognize differences correcly?

What am I missing here? Is there an option that XMLDiff should care about element names and seek for best match to recognize following changes correctly?
a Helper class for making comparisons between two XML files:
public class XMLDiffer
{
public XDocument Diff(string originalXML, string changedXML)
{
//http://msdn2.microsoft.com/en-us/library/aa302294.aspx
XmlDiff xmlDiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreWhitespace);
xmlDiff.Algorithm = XmlDiffAlgorithm.Precise;
StringBuilder diffgramStringBuilder = new StringBuilder();
bool xmlComparisonResult = false;
using (StringReader legacySr = new StringReader(originalXML), nextgenSr = new StringReader(changedXML))
{
using (XmlReader legacyReader = XmlReader.Create(legacySr), nextgenReader = XmlReader.Create(nextgenSr))
{
using (StringWriter sw = new StringWriter(diffgramStringBuilder))
{
using (XmlWriter diffgramWriter = XmlWriter.Create(sw))
{
xmlComparisonResult = xmlDiff.Compare(legacyReader, nextgenReader, diffgramWriter);
}
}
}
}
XDocument xdoc = XDocument.Parse(diffgramStringBuilder.ToString());
return xdoc;
}
public string GetChangeHtml(string originalXML, string changedXML)
{
XmlDiffView view = new XmlDiffView();
var diffgram = Diff(originalXML, changedXML);
string ret = "";
using (StringReader legacySr = new StringReader(originalXML), diffGramSr = new StringReader(diffgram.ToString()))
{
using (XmlReader legacyReader = XmlReader.Create(legacySr), diffgramReader = XmlReader.Create(diffGramSr))
{
using (StringWriter sw = new StringWriter())
{
view.Load(legacyReader, diffgramReader);
view.GetHtml(sw);
ret = sw.ToString();
}
}
}
return ret;
}
}
With Following test:
[TestMethod]
public void XMLDiff_AreNotSame_GetChangeHtmlAll()
{
//Arrange
string source = "<root><child>some text</child><child>more text</child><child1>REMOVED</child1></root>";
//Ordering of the generic child nodes is not changed, but it might
string target = "<root><child>some text CHANGE</child><child>more text</child><child>ADDITION</child></root>";
XMLDiffer differ = new XMLDiffer();
//Act
var diffview = differ.GetChangeHtml(source, target);
//Assert
Assert.IsNotNull(diffview);
}
Produces following (html and table elements added):
https://pste.eu/p/Fm7Z.html
More info about library: http://msdn2.microsoft.com/en-us/library/aa302294.aspx
Nuget link for references: https://www.nuget.org/packages/XMLDiffPatch/
I ended up implementing following classes to get changes:
public class XMLComparer : IEqualityComparer<XNode>
{
public bool Equals(XNode e1, XNode e2)
{
if (!(e1 is XElement)) return true;
if (!(e2 is XElement)) return false;
var el1 = e1 as XElement;
var el2 = e2 as XElement;
return Tuple.Create(el1.Name, el1.Value).Equals(Tuple.Create(el2.Name, el2.Value));
}
public int GetHashCode(XNode n)
{
if (!(n is XElement)) return 0;
var el = n as XElement;
return Tuple.Create(el.Name, el.Value).GetHashCode();
}
}
public class XMLDifference
{
public bool IsNew { get; set; }
public XElement Node { get; set; }
}
public class XMLDifferenceComparer
{
public List<XMLDifference> GetDifferences(string original, string changed)
{
List<XMLDifference> ret = new List<XMLDifference>();
var originalDoc = XDocument.Parse(original);
var changedDoc = XDocument.Parse(changed);
//Get differences that are present in new xml version
var differences = changedDoc.Root.Descendants().Except(originalDoc.Root.Descendants(), new XMLComparer());
ret.AddRange(GetList(differences, true));
//Get differences that have changed since the old xml version
var oldValues = originalDoc.Root.Descendants().Except(changedDoc.Root.Descendants(), new XMLComparer());
ret.AddRange(GetList(oldValues, false));
return ret;
}
private List<XMLDifference> GetList(IEnumerable<XNode> nodes, bool isNew)
{
List<XMLDifference> ret = new List<XMLDifference>();
foreach (XNode d in nodes)
{
var diff = new XMLDifference();
diff.IsNew = isNew;
var el = d as XElement;
diff.Node = el;
ret.Add(diff);
}
return ret;
}
}
This can recognize changes but is not element specific, it cannot map which element exactly was changed and how, caused by lack of unique identifiers for each element.
The main idea for this solution came from here: https://gist.github.com/krcourville/6933451

read line and keep spaces from xml file

I am trying to write in xml file some profiles that i created so far so good ,
the input string is ProfilesList(0) = "45 65 67"
ProfilesList(1) = "profilename";
public void CreateGroupXML(String GroupNameWithPath, List<String> ProfilesList)
{
ProfilesGroup.ProfilesList = ProfilesList;
XmlWriterSettings ws = new XmlWriterSettings();
ws.NewLineHandling = NewLineHandling.Entitize;
for (int i = 0; i < ProfilesList.Count; i++)
{
ProfilesList[i] += Environment.NewLine;
}
XmlSerializer serializer = new XmlSerializer(typeof(ProfilesGroup));
using (XmlWriter wr = XmlWriter.Create(GroupNameWithPath, ws))
{
serializer.Serialize(wr, ProfilesGroup);
}
}
}
in the xml file the profiles written like that :
ProfilesList="45 65 67
profilename
So far so good, the problem happens when i trying read from the xml file
it split the first profile name into 3
here the code
public List<string> getProfilesOfGroup(string groupNameFullPath)
{
Stream stream = null;
try
{
stream = File.OpenRead(groupNameFullPath);
XmlSerializer serializer = new XmlSerializer(typeof(ProfilesGroup));
_ProfilesGroup = (ProfilesGroup)serializer.Deserialize(stream);
stream.Close();
return _ProfilesGroup.ProfilesList;
}
catch (Exception Ex)
{
log.ErrorFormat("Exception in getProfilesOfGroup: {0}", Ex.Message);
if (stream != null)
{
stream.Close();
}
return null;
}
}
the output (lets call the string ProfileList) contains :
ProfileList(0) = 45
ProfileList(1) = 65
ProfileList(2) = 67
ProfileList(3) = profilename
and i expecting the string to contain
ProfileList(0) = 45 65 67
ProfileList(1) = profilename
edit here the full xml :
?xml version="1.0" encoding="utf-8"?ProfilesGroup
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" ProfilesList="45 65
67
profilename
"
and the class:
[XmlRootAttribute("VProfilesGroup", IsNullable = false, DataType = "", Namespace = "")]
public class ProfilesGroup
{
[XmlAttribute("ProfilesList")]
public List<String> ProfilesList = new List<string>();
}
Why not just remove the [XmlAttribute("ProfilesList")] attribute? Your data will be serialized and deserialized successfully. The XML will look like:
<VProfilesGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ProfilesList>
<string>45 65 67</string>
<string>profilename</string>
</ProfilesList>
</VProfilesGroup>
In this format the list of strings is clearly defined to have two entries. This is the standard way to serialize & deserialize an array of strings with XmlSerializer. Or do you have some external constraint making you declare the list as an attribute?
Update
If you must serialize the ProfilesList as an attribute not an array of elements, you can manually construct and deconstruct the string like so:
[XmlRootAttribute("VProfilesGroup", IsNullable = false, DataType = "", Namespace = "")]
public class ProfilesGroup
{
static readonly char Delimiter = '\n';
[XmlIgnore]
public List<String> ProfilesList { get; set; } // Enhance the setter to throw an exception if any string contains the delimiter.
[XmlAttribute("ProfilesList")]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string ProfilesListText
{
get
{
return string.Join(Delimiter.ToString(), ProfilesList.ToArray());
}
set
{
ProfilesList = new List<string>(value.Split(Delimiter));
}
}
public static string CreateGroupXML(List<String> ProfilesList)
{
var group = new ProfilesGroup();
group.ProfilesList = ProfilesList;
return XmlSerializationHelper.GetXml(group);
}
public static List<string> GetProfilesOfGroup(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(ProfilesGroup));
var group = (ProfilesGroup)serializer.Deserialize(new StringReader(xml));
return group == null ? null : group.ProfilesList;
}
public static void Test()
{
List<string> list = new List<string>(new string[] { "45 65 67", "profilename" });
var xml = CreateGroupXML(list);
var newList = GetProfilesOfGroup(xml);
bool same = list.SequenceEqual(newList);
Debug.Assert(same); // No assert.
}
}
The resulting XML looks like:
<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<VProfilesGroup xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ProfilesList=\"45 65 67
profilename\" />
In this case, I am testing the code by serializing and deserializing to a string rather than to a file. And then the helper:
public static class XmlSerializationHelper
{
public static string GetXml<T>(T obj, XmlSerializer serializer) where T : class
{
using (var textWriter = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; // For cosmetic purposes.
settings.IndentChars = " "; // The indentation used in the test string.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
serializer.Serialize(xmlWriter, obj);
}
return textWriter.ToString();
}
}
public static string GetXml<T>(T obj) where T : class
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return GetXml(obj, serializer);
}
}

How to serialize to xml using Linq

How can i serialize Instance of College to XML using Linq?
class College
{
public string Name { get; set; }
public string Address { get; set; }
public List<Person> Persons { get; set; }
}
class Person
{
public string Gender { get; set; }
public string City { get; set; }
}
You can't serialize with LINQ. You can use XmlSerializer.
XmlSerializer serializer = new XmlSerializer(typeof(College));
// Create a FileStream to write with.
Stream writer = new FileStream(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
Not sure why people are saying you can't serialize/deserialize with LINQ. Custom serialization is still serialization:
public static College Deserialize(XElement collegeXML)
{
return new College()
{
Name = (string)collegeXML.Element("Name"),
Address = (string)collegeXML.Element("Address"),
Persons = (from personXML in collegeXML.Element("Persons").Elements("Person")
select Person.Deserialize(personXML)).ToList()
}
}
public static XElement Serialize(College college)
{
return new XElement("College",
new XElement("Name", college.Name),
new XElement("Address", college.Address)
new XElement("Persons", (from p in college.Persons
select Person.Serialize(p)).ToList()));
);
Note, this probably isn't the greatest approach, but it's answering the question at least.
You can't use LINQ. Look at the below code as an example.
// This is the test class we want to
// serialize:
[Serializable()]
public class TestClass
{
private string someString;
public string SomeString
{
get { return someString; }
set { someString = value; }
}
private List<string> settings = new List<string>();
public List<string> Settings
{
get { return settings; }
set { settings = value; }
}
// These will be ignored
[NonSerialized()]
private int willBeIgnored1 = 1;
private int willBeIgnored2 = 1;
}
// Example code
// This example requires:
// using System.Xml.Serialization;
// using System.IO;
// Create a new instance of the test class
TestClass TestObj = new TestClass();
// Set some dummy values
TestObj.SomeString = "foo";
TestObj.Settings.Add("A");
TestObj.Settings.Add("B");
TestObj.Settings.Add("C");
#region Save the object
// Create a new XmlSerializer instance with the type of the test class
XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(#"C:\test.xml");
SerializerObj.Serialize(WriteFileStream, TestObj);
// Cleanup
WriteFileStream.Close();
#endregion
/*
The test.xml file will look like this:
<?xml version="1.0"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SomeString>foo</SomeString>
<Settings>
<string>A</string>
<string>B</string>
<string>C</string>
</Settings>
</TestClass>
*/
#region Load the object
// Create a new file stream for reading the XML file
FileStream ReadFileStream = new FileStream(#"C:\test.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
// Load the object saved above by using the Deserialize function
TestClass LoadedObj = (TestClass)SerializerObj.Deserialize(ReadFileStream);
// Cleanup
ReadFileStream.Close();
#endregion
// Test the new loaded object:
MessageBox.Show(LoadedObj.SomeString);
foreach (string Setting in LoadedObj.Settings)
MessageBox.Show(Setting);
you have to use the XML serialization
static public void SerializeToXML(College college)
{
XmlSerializer serializer = new XmlSerializer(typeof(college));
TextWriter textWriter = new StreamWriter(#"C:\college.xml");
serializer.Serialize(textWriter, college);
textWriter.Close();
}
You can use that if you needed XDocument object after serialization
DataClass dc = new DataClass();
XmlSerializer x = new XmlSerializer(typeof(DataClass));
MemoryStream ms = new MemoryStream();
x.Serialize(ms, dc);
ms.Seek(0, 0);
XDocument xDocument = XDocument.Load(ms); // Here it is!
I'm not sure if that is what you want, but to make an XML-Document out of this:
College coll = ...
XDocument doc = new XDocument(
new XElement("College",
new XElement("Name", coll.Name),
new XElement("Address", coll.Address),
new XElement("Persons", coll.Persons.Select(p =>
new XElement("Person",
new XElement("Gender", p.Gender),
new XElement("City", p.City)
)
)
)
);

XmlSerializer and controlling namespace in XmlAnyElement

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

Categories

Resources