We've already established previously that DCS serializes/deserializes objects alphabetically. However, after further investigation I've discoverred this is not entirely true.
If we have a structure like this:
[DataContract]
public class Content
{
[DataMember]
public string Title;
[DataMember]
public string Slug;
[DataMember]
public string Description;
}
[DataContract]
public class TextContent : Content
{
[DataMember]
public string Text;
}
and have an object of type TextContent to serialize, DCS will do this:
<Content i:type="TextContent" ...>
<Description>desc</Description>
<Slug>some-slug</Slug>
<Title>content title</Title>
<Text>some content</Text>
</Content>
So as you can see, the property from the inheriting class is attached to the end of the serialized XML fragment even though it should be before Title. DCS doesn't go over the combined properties and reorder them.
I've noticed this when I was manually adding Text element in front of Title element and deserialization just didn't want to work. That's why I performed a serialization of a new object and figured this out.
My questions are:
This can't be common knowledge?? Anyone else noticed this?
Anyone knows of a better serializer (all I ever find if I search for it is the old XmlSerializer and DCS) because this issue with DCS's ordering is extremely annoying? I know we can use the Order attribute but that only enables us to align with one external XML source. What if we have three, four or more third party XML providers which all generate perfectly valid XML but our app is nitpicking about elements order (because of DCS)?
The base types are always first in the order. You can define the order of the serialized properties of a object with respect of the Order property of the DataMember attribute (see http://msdn.microsoft.com/en-us/library/ms729813.aspx)
There's the NetDataContractSerializer but the only difference between it and the DCS is that it enables type sharing between the client and the server but you lose the forward compatibility because both sides have to serialize/deserialize into the same type..
There's also the C# wrapper for protocol buffer on codeplex:
http://code.google.com/p/protobuf-net/
I haven't tried it out myself, but it's supposed to be much faster and lightweight. As to your actual questions:
doubt it, I certainly never
noticed this :-P
can you give an example where the ordering of elements actually mattered? I haven't come across this myself (I guess that's why most of us haven't noticed this behavior..), but with the proto-buf serializer this will undoubtly be a problem..
If you need to be able to serialize to match an external schema, then you obviously shouldn't be using the DataContractSerializer. That's not what it's for.
You can either use the XmlSerializer, which is intended to give you more control over the serialized XML, or implement IXmlSerializable, and gain complete control over the XML, or you can write your own custom serialization using LINQ to XML. This will let you do exactly what you mention - serialize the same data in different ways. Example:
Data
internal class Person
{
internal string Name { get; set; }
internal string Telephone { get; set; }
internal Address HomeAddress { get; set; }
internal Address WorkAddress { get; set; }
}
internal class Address
{
internal string Line1 { get; set; }
internal string Line2 { get; set; }
internal string City { get; set; }
internal string State { get; set; }
internal string PostalCode { get; set; }
}
Test Program
private static void Main()
{
var person = new Person
{
Name = "John Saunders",
Telephone = "something",
HomeAddress = new Address
{
Line1 = "Line 1",
Line2 = "Line 2",
City = "SomeCity",
State = "SS",
PostalCode = "99999-9999",
},
WorkAddress = new Address
{
Line1 = "Line 1a",
Line2 = "Line 2a",
City = "SomeCitay",
State = "Sa",
PostalCode = "99999-999a",
},
};
XDocument personWithElements = SerializeAsElements(person);
personWithElements.Save("PersonWithElements.xml");
XDocument personWithAttributes = SerializeAsAttributes(person);
personWithAttributes.Save("PersonWithAttributes.xml");
}
Serialization as Elements:
private static XDocument SerializeAsElements(Person person)
{
return new XDocument(
new XElement("Person",
new XElement("Name", person.Name),
new XElement("Telephone", person.Telephone),
SerializeAddressAsElements(person.HomeAddress, "HomeAddress"),
SerializeAddressAsElements(person.WorkAddress, "WorkAddress"))
);
}
private static XElement SerializeAddressAsElements(Address address, string elementName)
{
return new XElement(elementName,
new XElement("Line1", address.Line1),
new XElement("Line2", address.Line2),
new XElement("City", address.City),
new XElement("State", address.State),
new XElement("PostalCode", address.PostalCode)
);
}
Serialization as Attributes:
private static XDocument SerializeAsAttributes(Person person)
{
return new XDocument(
new XElement("Person",
new XAttribute("Name", person.Name),
new XAttribute("Telephone", person.Telephone),
SerializeAddressAsAttributes(person.HomeAddress, "HomeAddress"),
SerializeAddressAsAttributes(person.WorkAddress, "WorkAddress"))
);
}
private static XElement SerializeAddressAsAttributes(Address address, string elementName)
{
return new XElement(elementName,
new XAttribute("Line1", address.Line1),
new XAttribute("Line2", address.Line2),
new XAttribute("City", address.City),
new XAttribute("State", address.State),
new XAttribute("PostalCode", address.PostalCode)
);
}
PersonWithElements:
<?xml version="1.0" encoding="utf-8"?>
<Person>
<Name>John Saunders</Name>
<Telephone>somethine</Telephone>
<HomeAddress>
<Line1>Line 1</Line1>
<Line2>Line 2</Line2>
<City>SomeCity</City>
<State>SS</State>
<PostalCode>99999-9999</PostalCode>
</HomeAddress>
<WorkAddress>
<Line1>Line 1a</Line1>
<Line2>Line 2a</Line2>
<City>SomeCitay</City>
<State>Sa</State>
<PostalCode>99999-999a</PostalCode>
</WorkAddress>
</Person>
PersonWithAttributes:
<?xml version="1.0" encoding="utf-8"?>
<Person Name="John Saunders" Telephone="somethine">
<HomeAddress Line1="Line 1" Line2="Line 2" City="SomeCity" State="SS" PostalCode="99999-9999" />
<WorkAddress Line1="Line 1a" Line2="Line 2a" City="SomeCitay" State="Sa" PostalCode="99999-999a" />
</Person>
Related
I do have a problem with serializing a ArrayList. Most propably I use wrong XML Attributes for it since I when I changed them it would not even serialize it and got errors like 'The type may not be used in this context.'
I need to use a non generic ArrayList. On adding [XmlArray("LineDetails")] made this code to run but the output is not correct, it should give me the LineDetails structure. Any idea how to fix this?
This is a part of a whole xml like Document > Header > LineCollection > LineDeatails.
The problem is only with this details if I use a standard string field it is ok but the range of the colletion if changing with every document.
[XmlType(TypeName = "LineCollection")]
public class LineCollection
{
public String LineCount{ get; set; }
// [XmlElement(ElementName = "LineDetails")]
[XmlArray("LineDetails")]
public ArrayList LineDetails{ get; set; }
}
public class LineDetails: ArrayList
{
public String LineNum{ get; set; }
public String ItemId{ get; set; }
public String ItemName{ get; set; }
//... only strings
}
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => new UTF8Encoding(false);
}
public string Serialize()
{
// var xmlserializer = new XmlSerializer(this.GetType());
var xmlserializer = new XmlSerializer(this.GetType(), new Type[] { typeof(LineDetails) });
var Utf8StringWriter = new Utf8StringWriter();
var xns = new XmlSerializerNamespaces();
xns.Add(string.Empty, string.Empty);
using (var writer = XmlWriter.Create(Utf8StringWriter))
{
xmlserializer.Serialize(writer, this, xns);
return Utf8StringWriter.ToString();
}
}
And the incorrect output of this...
<LineColletion>
<LineDetails>
<anyType xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" p5:type="ArrayOfAnyType"/>
<anyType xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" p5:type="ArrayOfAnyType"/>
<anyType xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" p5:type="ArrayOfAnyType"/>
</LineDetails>
</LineColletion>
it should be like this
<LineColletion>
<LineDetails>
<LineNum>1</LineNum>
<ItemId>Item_2321</ItemId>
<ItemName>TheItemName</ItemName>
</LineDetails>
<LineDetails>
<LineNum>2</LineNum>
<ItemId>Item_232100000</ItemId>
<ItemName>TheItemName0</ItemName>
</LineDetails>
<LineDetails>
<LineNum>3</LineNum>
<ItemId>Item_23217777</ItemId>
<ItemName>TheItemName7</ItemName>
</LineDetails>
</LineColletion>
Now the wrong xml looks like this...
<LineDetails>
<anyType xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" p5:type="LineDetails">
<LineNum>1</LineNum>
<ItemId>Item_2321</ItemId>
<ItemName>TheItemName</ItemName>
</anyType>
<anyType xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" p5:type="LineDetails">
<LineNum>2</LineNum>
<ItemId>Item_2321</ItemId>
<ItemName>TheItemName</ItemName>
</anyType>
</LineDetails>
You may generate the required XML by modifying your data model as follows:
[XmlType(TypeName = "LineColletion")] // Fixed: TypeName. But do you want LineColletion (misspelled) or LineCollection (correctly spelled)? Your XML shows LineColletion but your code used LineCollection.
public class LineCollection
{
public String LineCount{ get; set; }
[XmlElement("LineDetails", typeof(LineDetails))] // Fixed -- specify type(s) of items in the ArrayList.
public ArrayList LineDetails{ get; set; }
}
public class LineDetails // Fixed: removed inheritance from ArrayList.
{
public String LineNum{ get; set; }
public String ItemId{ get; set; }
public String ItemName{ get; set; }
//... only strings
}
Notes:
Your model makes LineDetails inherit from ArrayList. XmlSerializer will never serialize collection properties, it will only serialize collection items. In order to serialize it correctly, I removed the inheritance since you don't seem to be using it anyway.
If you really need LineDetails to inherit from ArrayList, you will need to implement IXmlSerializable or replace it collection with a DTO.
Implementing IXmlSerializable is tedious and error-prone. I don't recommend it.
Your LineDetails collection is serialized without an outer wrapper element. To make the serializer do this, apply XmlElementAttribute to the property.
ArrayList is an untyped collection, so you must inform XmlSerializer of the possible types it might contain. You have two options:
Assigning a specific type to a specific element name by setting XmlElementAttribute.Type (or XmlArrayItemAttribute.Type), OR
Adding xsi:type attributes by informing the serializer of additional included types. You are doing this by passing them into the constructor, which is why you are seeing the p5:type="LineDetails" attribute.
Since you don't want the attributes, you need to set the element name by setting the type like so:
[XmlElement("LineDetails", typeof(LineDetails))]
The XML element corresponding to your LineCollection is named <LineColletion>. Note that the spelling is inconsistent. You will need to set [XmlType(TypeName = "LineColletion")] to the name you actually want.
Demo fiddle here.
I am retrieving XML documents from a web service I have no control over. The XML is formatted similarly to the following:
<?xml version="1.0"?>
<ns:obj xmlns:ns="somenamespace">
<address>1313 Mockingbird Lane</address>
<residents>5</residents>
</ns:obj>
where the root node is in the "ns" namespace, but none of its child elements are.
After some trial and error, I found that I could deserialize the document to a C# object by doing the following:
[XmlRoot(Namespace="somenamespace", ElementName="obj")]
public class xmlObject
{
[XmlElement(Namespace = "")]
public string address { get; set; }
[XmlElement(Namespace = "")]
public int residents { get; set; }
}
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\"?>" +
"<ns:obj xmlns:ns=\"somenamespace\">" +
" <address>1313 Mockingbird Lane</address>" +
" <residents>5</residents>" +
"</ns:obj>";
var serializer = new XmlSerializer(typeof(xmlObject));
using (var reader = new StringReader(xml))
{
var result = serializer.Deserialize(reader) as xmlObject;
Console.WriteLine("{0} people live at {1}", result.residents, result.address);
// Output: "5 people live at 1313 Mockingbird lane"
}
}
}
If I omit the XmlElementAttribute on the individual members, I instead get an empty object. I.e. The output reads
0 people live at
(result.address is equal to null.)
I understand the rationale behind why the deserialization process works like this, but I'm wondering if there is a less verbose way to tell XmlSerializer that the child elements of the object are not in the same namespace as the root node.
The objects I'm working with in production have dozens of members and, for cleanliness sake, I'd like to avoid tagging all of them with [XmlElement(Namespace = "")] if it's easily avoidable.
You can combine XmlRootAttribute with XmlTypeAttribute to make it so the root element, and the root element's elements, have different namespaces:
[XmlRoot(Namespace="somenamespace", ElementName="obj")]
[XmlType(Namespace="")]
public class xmlObject
{
public string address { get; set; }
public int residents { get; set; }
}
Using the type above, if I deserialize and re-serialize your XML I get:
<q1:obj xmlns:q1="somenamespace">
<address>1313 Mockingbird Lane</address>
<residents>5</residents>
</q1:obj>
Sample fiddle.
If you know the contract with the web service, why not use a DataContractSerializer to deserialize the XML into the objects?
I want to deserialize XML to object in C#, object has one string property and list of other objects.
There are classes which describe XML object, my code doesn't work (it is below, XML is at end of my post). My Deserialize code doesn't return any object.
I think I do something wrong with attributes, could you check it and give me some advice to fix it.
Thanks for your help.
[XmlRoot("shepherd")]
public class Shepherd
{
[XmlElement("name")]
public string Name { get; set; }
[XmlArray(ElementName = "sheeps", IsNullable = true)]
[XmlArrayItem(ElementName = "sheep")]
public List<Sheep> Sheeps { get; set; }
}
public class Sheep
{
[XmlElement("colour")]
public string colour { get; set; }
}
There is C# code to deserialize XML to objects
var rootNode = new XmlRootAttribute();
rootNode.ElementName = "createShepherdRequest";
rootNode.Namespace = "http://www.sheeps.pl/webapi/1_0";
rootNode.IsNullable = true;
Type deserializeType = typeof(Shepherd[]);
var serializer = new XmlSerializer(deserializeType, rootNode);
using (Stream xmlStream = new MemoryStream())
{
doc.Save(xmlStream);
var result = serializer.Deserialize(xmlStream);
return result as Shepherd[];
}
There is XML example which I want to deserialize
<?xml version="1.0" encoding="utf-8"?>
<createShepherdRequest xmlns="http://www.sheeps.pl/webapi/1_0">
<shepherd>
<name>name1</name>
<sheeps>
<sheep>
<colour>colour1</colour>
</sheep>
<sheep>
<colour>colour2</colour>
</sheep>
<sheep>
<colour>colour3</colour>
</sheep>
</sheeps>
</shepherd>
</createShepherdRequest>
XmlRootAttribute does not change the name of the tag when used as an item. The serializer expects <Shepherd>, but finds <shepherd> instead. (XmlAttributeOverrides does not seem to work on arrays either.) One way to to fix it, is by changing the case of the class-name itself:
public class shepherd
{
// ...
}
An easier alternative to juggling with attributes, is to create a proper wrapper class:
[XmlRoot("createShepherdRequest", Namespace = "http://www.sheeps.pl/webapi/1_0")]
public class CreateShepherdRequest
{
[XmlElement("shepherd")]
public Shepherd Shepherd { get; set; }
}
I'm working on a web service that needs to accept a collection with three values of different types. The values are
SkuNumber (integer),
FirstName (string),
LastName (string)
I want the web service to accept a list of 100 instances of these values but am not sure how to go about it. Do I use a multidimensional list or array? Maybe a tuple? Or can I just create a simple class structure and accept a list of that class?
This is all simple enough in a normal application, I'm just not sure how the app calling the web service would pass the data with any of the given options.
Can someone give me some pointers?
If a shared assembly is not feasible, you can always go with good ol' XML. It may not be the optimal solution and I'm sure plenty of users here will balk at the idea, but it is easy to support and relatively quick to implement, so it really depends on your individual situation and the skill level of the developers responsible for supporting the application.
The benefit to using XML here, is that the calling application can be written in almost any language on almost any platform, as long as it adheres to the XML structure.
The XML string should be easy enough to generate in the calling application, but the biggest downside here is that if you have a ton of data, the processing may take longer than desired -- on both ends of the service.
Here is a working sample if you want to give it a try:
public class Whatever
{
public int SkuNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[WebMethod]
public void HelloWorld(string xmlString)
{
//make all the node names + attribute names lowercase, to account for erroneous xml formatting -- leave the values alone though
xmlString = Regex.Replace(xmlString, #"<[^<>]+>", m => m.Value.ToLower(),RegexOptions.Multiline | RegexOptions.Singleline);
var xmlDoc = LoadXmlDocument(xmlString);
var listOfStuff = new List<Whatever>();
var rootNode = xmlDoc.DocumentElement;
foreach(XmlNode xmlNode in rootNode)
{
var whatever = new Whatever
{
FirstName = xmlNode["first_name"].InnerText,
LastName = xmlNode["last_name"].InnerText,
SkuNumber = Convert.ToInt32(xmlNode["sku_number"].InnerText)
};
listOfStuff.Add(whatever);
}
}
public static XmlDocument LoadXmlDocument(string xmlString)
{
//some extra stuff to account for URLEncoded strings, if necessary
if (xmlString.IndexOf("%3e%") > -1)
xmlString = HttpUtility.UrlDecode(xmlString);
xmlString = xmlString.Replace((char)34, '\'').Replace("&", "&").Replace("\\", "");
var xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = false;
xmlDocument.LoadXml(xmlString);
return xmlDocument;
}
Your XML would look like this:
<stuff_to_track>
<whatever>
<sku_number>1</sku_number>
<first_name>jimi</first_name>
<last_name>hendrix</last_name>
</whatever>
<whatever>
<sku_number>2</sku_number>
<first_name>miles</first_name>
<last_name>davis</last_name>
</whatever>
<whatever>
<sku_number>3</sku_number>
<first_name>david</first_name>
<last_name>sanborn</last_name>
</whatever>
<whatever>
<sku_number>4</sku_number>
<first_name>john</first_name>
<last_name>coltrane</last_name>
</whatever>
</stuff_to_track>
I also recommend validating the incoming XML, for both schema and data.
Create a class and accept a list of that class. Be sure to mark it as [Serializable].
[Serializable]
public class Whatever
{
public int SkuNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Best practice would be to define the class in an assembly that can be accessed by both the service and the project that calls it.
The trouble with a tuple or a multi-dimensional array is that the data you send doesn't have an inherent identity: you could stick any old thing in there. If you have a class, you are indicating that you are sending an Order or an Inquiry or a Coupon or whatever it is you are tracking. There's a level of meaning that goes along with it.
Just send what you want:
public class Whatever
{
public int SkuNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
[WebMethod]
public void TakeList(List<Whatever> theList)
{
foreach (var w in theList)
{
}
}
I have an XML document:
<data>
<elmt1>Element 1</elmt1>
<elmnt2>Element 2</elmnt2>
<elmnt3>Element 3</elmnt3>
</data>
I need to deserialize to an object that serializes to a different root name with everything else remaining the same.
For example:
<dataNew>
<elmt1>Element 1</elmt1>
<elmnt2>Element 2</elmnt2>
<elmnt3>Element 3</elmnt3>
</dataNew>
When serializing, we can always apply XmlRootAttribute to serialize to a different root name but I am not sure how to deserialize to a different XmlRootAttribute. It keeps failing error in document (1,2) pointing to the root attribute.
How can I achieve that?
If it's only the root name you want to change you can specify the root attribute when declaring the XmlSerializer.
XmlSerializer xmlSerializer = new XmlSerializer(typeof(data), new XmlRootAttribute("dataNew"));
XmlRootAttribute was supposed to work
[XmlRoot("dataNew")]
public class MyData()
{
[XmlElement("elmt1")]
public string myElement1{get;set;}
[XmlElement("elmnt2")]
public string myElement2{get;set;}
[XmlElement("elmtn3")]
public string myElement3{get;set;}
}
EDIT: Completed the XML
Did you try using the XmlAttributeOverrides class?
a sample of using XmlAttributeOverrides. If you vote up give one to hjb417 as well
class Program
{
static void Main(string[] args)
{
using (var fs = File.OpenRead("XmlFile1.xml"))
using (var fs2 = File.OpenRead("XmlFile2.xml"))
{
var xSer = new XmlSerializer(typeof(data));
var obj = xSer.Deserialize(fs);
//
var xattribs = new XmlAttributes();
var xroot = new XmlRootAttribute("dataNew");
xattribs.XmlRoot = xroot;
var xoverrides = new XmlAttributeOverrides();
xoverrides.Add(typeof(data), xattribs);
var xSer2 = new XmlSerializer(typeof(data), xoverrides);
var obj2 = xSer2.Deserialize(fs2);
}
}
}
public class data
{
public string elmt1 { get; set; }
public string elmnt2 { get; set; }
public string elmnt3 { get; set; }
}
You can use ExtendedXmlSerializer. This serializer support change root element name and property name.
If you have class like this:
[XmlRoot("dataNew")]
public class Data
{
[XmlElement("elmt1")]
public string Element1 { get; set; }
[XmlElement("elmnt2")]
public string Element2 { get; set; }
[XmlElement("elmtn3")]
public string Element3 { get; set; }
}
You can serialize it:
ExtendedXmlSerializer serializer = new ExtendedXmlSerializer();
var obj = new Data
{
Element1 = "A",
Element2 = "B",
Element3 = "C",
};
var xml = serializer.Serialize(obj);
Your xml will look like:
<?xml version="1.0" encoding="utf-8"?>
<dataNew type="Models.Example">
<elmt1>A</elmt1>
<elmnt2>B</elmnt2>
<elmtn3>C</elmtn3>
</dataNew>
ExtendedXmlSerializer has many other useful features:
Deserialization xml from standard XMLSerializer
Serialization class with property interface
Serialization circular reference and reference Id
Deserialization of old version of xml
Property encryption
Custom serializer
ExtendedXmlSerializer supports .net 4.5 and .net Core. You can integrate it with WebApi and AspCore.
You might have to implement ISerializable and change the root element in GetObjectData().