C# Soap XML SerializationException while parsing to object - c#

When i try to parse soap xml to objects, i'm getting below exception not sure what i'm doing wrong.
Exception:
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll
Additional information: Error in line 1 position 687. Element 'http://soap.sforce.com/2005/09/outbound:sObject' contains data from a type that maps to the name 'urn:sobject.enterprise.soap.sforce.com:Contact'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'Contact' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.
static void Main(string[] args)
{
string inputString = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?> <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <soapenv:Body> <notifications xmlns=\"http://soap.sforce.com/2005/09/outbound\"> <SessionId xsi:nil=\"true\"/> <EnterpriseUrl>https://hultibs--FullDev.cs10.my.salesforce.com/services/Soap/c/44.0/00DJ0000003QX7f</EnterpriseUrl> <PartnerUrl>https://hultibs--FullDev.cs10.my.salesforce.com/services/Soap/u/44.0/00DJ0000003QX7f</PartnerUrl> <Notification> <Id>04lJ000000PoRS2IAN</Id> <sObject xsi:type=\"sf:Contact\" xmlns:sf=\"urn:sobject.enterprise.soap.sforce.com\"> <sf:Id>0033600001koT9qAAE</sf:Id> <sf:Email>tcampbell2018#maili.com</sf:Email> <sf:Student_ID__c>5192435</sf:Student_ID__c> </sObject> </Notification> </notifications> </soapenv:Body> </soapenv:Envelope>";
FromXml(inputString);
Console.ReadLine();
}
public static void FromXml(string Xml)
{
using (var reader = XmlReader.Create(new StringReader(Xml)))
{
Message m = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
var body = m.GetBody<Notifications>();
Console.WriteLine(body);
}
}
[DataContract(Name = "sObject", Namespace = "http://soap.sforce.com/2005/09/outbound")]
public class SObject
{
[DataMember(Name = "Id", Order = 1)]
public string Id { get; set; }
[DataMember(Name = "Email", Order = 2)]
public string Email { get; set; }
[DataMember(Name = "Student_ID__c", Order = 3)]
public string Student_ID__c { get; set; }
[DataMember(Name = "type", Order = 4)]
public string Type { get; set; }
[DataMember(Name = "sf", Order = 5)]
public string Sf { get; set; }
}
[DataContract(Name = "Notification", Namespace = "http://soap.sforce.com/2005/09/outbound")]
public class Notification
{
[DataMember(Name = "Id", Order = 1)]
public string Id { get; set; }
[DataMember(Name = "sObject", Order = 2)]
public SObject SObject { get; set; }
}
[DataContract(Name = "notifications", Namespace = "http://soap.sforce.com/2005/09/outbound")]
public class Notifications
{
[DataMember(Name = "ActionId", Order = 2)]
public string ActionId { get; set; }
[DataMember(Name = "EnterpriseUrl", Order = 3)]
public string EnterpriseUrl { get; set; }
[DataMember(Name = "PartnerUrl", Order = 4)]
public string PartnerUrl { get; set; }
[DataMember(Name = "Notification", Order = 5)]
public Notification Notification { get; set; }
}

The problem, which is described in the exception, is in the following type and namespace declaration for sObject in the soap message
<sObject xsi:type=\"sf:Contact\" xmlns:sf=\"urn:sobject.enterprise.soap.sforce.com\">
because there is no class Contact defined in that namespace (or any other).
If you remove the type and namespace declaration from sObject in the soap message (and remove the sf: prefix from its members) it should work OK.
Or remove the xsi:type=\"sf:Contact\ and change the DataContract to
[DataContract(Name = "sObject", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
Or leave the soap message as it is and change
[DataContract(Name = "sObject", Namespace = "http://soap.sforce.com/2005/09/outbound")]
public class SObject
to
[DataContract(Name = "Contact", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
public class Contact
also changing (in Notification)
[DataMember(Name = "sObject", Order = 2)]
public SObject SObject { get; set; }
to
[DataMember(Name = "sObject", Order = 2)]
public Contact SObject { get; set; }

You only declare a namespace "http://soap.sforce.com/2005/09/outbound" in your DataContract, you could use Message.CreateMessage to serialize your Notifications and compare your xml with the serialized message.
Below is the code.
static void Main(string[] args)
{
Notifications notifications = new Notifications()
{
ActionId = "actionId",
EnterpriseUrl = "enterpriceUri",
PartnerUrl = "parentUri",
Notification = new Notification
{
Id = "abc",
SObject = new SObject
{
Email = "email",
Id = "id",
Sf = "sf",
Student_ID__c = "a",
Type = "type"
}
}
};
Message me = Message.CreateMessage(MessageVersion.Soap11, "www.abc.com", notifications); // create a message and serialize the notifications into the message
WriteMessage(me, #"d:\message.xml");
}
static void WriteMessage(Message message, string fileName)
{
using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
{
message.WriteMessage(writer);// write the message into a file
}
Process.Start(fileName);// show the file
}
And the serialized messsage.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">www.abc.com</Action></s:Header><s:Body><notifications xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://soap.sforce.com/2005/09/outbound"><ActionId>actionId</ActionId><EnterpriseUrl>enterpriceUri</EnterpriseUrl><PartnerUrl>parentUri</PartnerUrl><Notification><Id>abc</Id><sObject><Id>id</Id><Email>email</Email><Student_ID__c>a</Student_ID__c><type>type</type><sf>sf</sf></sObject></Notification></notifications></s:Body></s:Envelope>

Related

Is there smart way to parse xml to list?

There is xml file, which have next structure:
<?xml version="1.0"?>
<myobjectlist objectCount = "3">
<myobject Number = "0" Name="My First Object" MyChildObectsCount = "2">
<intProp>5</intProp>
<stringProp>Str1</stringProp>
<doubleProp>35.1</doubleProp>
<MyChildObj Number = "100" Name = "My first child object">
<childIntProp>1</childIntProp>
<childStringProp>CStr1</childStringProp>
</MyChildObj>
<MyChildObj Number = "120" Name = "My child object">
<childIntProp>15</childIntProp>
<childStringProp>CStr2</childStringProp>
</MyChildObj>
</myobject>
<myobject Number = "145" Name="My second Object" MyChildObectsCount = "1">
<intProp>96</intProp>
<stringProp>Str2</stringProp>
<doubleProp>+Inf</doubleProp>
<MyChildObj Number = "250" Name = "This's child object">
<childIntProp>62</childIntProp>
<childStringProp>CStr3</childStringProp>
</MyChildObj>
</myobject>
<myobject Number = "261" Name="My last Object" MyChildObectsCount = "3">
<intProp>9</intProp>
<stringProp>Str45</stringProp>
<doubleProp>1.6449635e+07</doubleProp>
<MyChildObj Number = "150" Name = "Almost last child object">
<childIntProp>-1</childIntProp>
<childStringProp>CStr41</childStringProp>
</MyChildObj>
<MyChildObj Number = "680" Name = "Prelast child object">
<childIntProp>72</childIntProp>
<childStringProp>CStr42</childStringProp>
</MyChildObj>
<MyChildObj Number = "127" Name = "Last child object">
<childIntProp>64</childIntProp>
<childStringProp>CStr222</childStringProp>
</MyChildObj>
</myobject>
</myobjectlist>
I've tried to use XMLSerializer, but how I found out it can't deserialize to list, all objects of the list will be the first object of deserializable xml, so me result list will consist ObjCount of 0th obj.
I've created classes -- enteties of xml objects, let's say
public class MyObject{
int Number;
string Name;
int IntProp;
string stringProp;
double doubleProp;
List<MyChildObject> myChildObjects;
}
public class MyChildObject{
int Number;
string Name;
int childIntProp;
string childStringProp;
}
I need to get List<MyObject> from XML file, but I do not want to parse it node by node. Is there smart way to do it?
UPDATE
And here, what I got: Empty list
If you annotate your data model like this:
[XmlRoot(ElementName = "myobjectlist")]
public class MyObjectList
{
[XmlAttribute(AttributeName = "objectCount")]
public string ObjectCount { get; set; }
[XmlElement(ElementName = "myobject")]
public List<MyObject> MyObjects { get; set; }
}
[XmlRoot(ElementName = "myobject")]
public class MyObject
{
[XmlAttribute]
public string Number { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute(AttributeName = "MyChildObectsCount")]
public string MyChildObjectsCount { get; set; }
[XmlElement(ElementName = "intProp")]
public string IntProp { get; set; }
[XmlElement(ElementName = "stringProp")]
public string StringProp { get; set; }
[XmlElement(ElementName = "doubleProp")]
public string DoubleProp { get; set; }
[XmlElement(ElementName = "MyChildObj")]
public List<MyChildObject> MyChildObjects { get; set; }
}
[XmlRoot(ElementName = "MyChildObj")]
public class MyChildObject
{
[XmlAttribute]
public string Number { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlElement(ElementName = "childIntProp")]
public string ChildIntProp { get; set; }
[XmlElement(ElementName = "childStringProp")]
public string ChildStringProp { get; set; }
}
The the deseralization logic is this simple:
StreamReader reader = new StreamReader(File.OpenRead("sample.xml"));
var serializer = new XmlSerializer(typeof(MyObjectList));
var data = (MyObjectList)serializer.Deserialize(reader);
UPDATE #1
UPDATE #2
If you need to handle
+Inf as float.PositiveInfinity
-Inf as float.NegativeInfinity
then you need to change the text of the <doubleProp>. One way to do this is the following:
var malformedXml = File.ReadAllText("sample.xml");
var fixedXml = malformedXml
.Replace("<doubleProp>+Inf</doubleProp>", "<doubleProp>Infinity</doubleProp>")
.Replace("<doubleProp>-Inf</doubleProp>", "<doubleProp>-Infinity</doubleProp>");
var fixedXmlStream = new MemoryStream();
var writer = new StreamWriter(fixedXmlStream);
writer.Write(fixedXml);
writer.Flush();
fixedXmlStream.Position = 0;
var reader = new StreamReader(fixedXmlStream);

Assign values from service response to list of objects

I have the following code in which I am getting result from service as below :
var result=CallService();
response.Alllist = new List<Check>
{
new Check
{
Bundle1 = new Bundle
{
Documents = new List<Document>
{
new Document(), new Document()
}
},
},
new CheckList
{
Bundle1 = new Bundle
{
Documents = new List<Document>
{
new Document(), new Document()
}
},
}
And I am struggling in assigning values to this.
And the response class is
public class Response
{
[DataMember(Order = 1)]
public bool Response { get; set; }
[DataMember(Order = 2)]
public List<Check> Alllist { get; set; }
}
public class Document
{
[DataMember(Order = 1)]
public string DocumentType { get; set; }
[DataMember(Order = 2)]
public string DocumentName { get; set; }
}
public class Bundle
{
[DataMember(Order = 1)]
public string BundleName { get; set; }
[DataMember(Order = 2)]
public string DocumentCategory { get; set; }
[DataMember(Order = 3)]
public string NextBundleName { get; set; }
[DataMember(Order = 4)]
public List<Document> Documents { get; set; }
}
public class Check
{
[DataMember(Order = 2)]
public string TransactionID { get; set; }
[DataMember(Order = 4)]
public Bundle Bundle1 { get; set; }
}
And the service returns ,two instances of system.collection.generic.list with multiple instances. and it returns the values of
BundleName,
DocumentCategory ,
NextBundleName ,
DocumentType ,
DocumentName.
How to take result value and assign to this response?
I am trying to assign like this
int count=0;
foreach (var c in result)
{
response.Alllist[count].Bundle1.BundleName = c
}
but since result is dynamic , I am not able to fetch value as c.BundleName
If response.Alllist is a List<Check> as you demonstrated in your first code block, you can populate the values in this manner:
response.Alllist[0].Bundle1.DocumentCategory = "my category";
response.Alllist[0].Bundle1.Documents[0].DocumentName = "my doc name";
and so on.
Other than that, I really don't know what else to tell you. I'm assuming you know about addressing collections by index, etc. You just have to look at the class composition hierarchy in your second code block, i.e. what classes contain instances or collections of other classes.
Based on my understanding of your question, In similar situation, I have tried something like below. Hope this helps. myclass is class define in my project containing field.
Guid testGuid = guid.empty;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(testData.customerAccountURL).Result;
if (response.IsSuccessStatusCode)
{
string JSONResponse = response.Content.ReadAsStringAsync().Result;
var rObjects = JsonConvert.DeserializeObject<List<myclass>>(JSONResponse);
testGuid = Guid.Parse(rObjects.First().field1.ToString());
// now use this guid to search for a customer
}
string GuidURL = URL + "/"+ testGuid;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(GuidURL);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
string data = values.ElementAt(0).Value;
}
}

how to send integer type data in xml

I am creating XML to send data. The data contains multiple datatypes as string, integer and decimal. My XML format and c# code to create it as follows.
<root>
<data>
<struct>
<PartnerCD></PartnerCD>
<UserName> </UserName>
<Password> </Password>
<Action> </Action>
<OfficeCD></OfficeCD>
<ChannelCD></ChannelCD>
<Token></Token>
<Notes> </Notes>
<Products>
<Product>
<ProductID></ProductID>
<SVA></SVA>
<Amount></Amount>
</Product>
</Products>
</struct>
</data>
</root>
And my c# code is
public static string CreateRequestXML(string partnerCd, string userName, string password, string action, string productId, string token, string sva, string amount)
{
XmlDocument doc = new XmlDocument();
XmlElement elemRoot = doc.CreateElement("root");
XmlElement elemData = doc.CreateElement("data");
XmlElement elemStruct = doc.CreateElement("struct");
XmlElement elemProducts = doc.CreateElement("Products");
XmlElement elemProduct = doc.CreateElement("Product");
doc.AppendChild(elemRoot);
elemRoot.AppendChild(elemData);
elemData.AppendChild(elemStruct);
//Insert data here
InsertDataNode(doc, elemStruct, "PartnerCD", partnerCd);
InsertDataNode(doc, elemStruct, "UserName", userName);
InsertDataNode(doc, elemStruct, "Password", password);
InsertDataNode(doc, elemStruct, "Action", action);
InsertDataNode(doc, elemStruct, "Token", token);
elemStruct.AppendChild(elemProducts);
elemProducts.AppendChild(elemProduct);
InsertDataNode(doc, elemProduct, "ProductID", productId);
InsertDataNode(doc, elemProduct, "SVA", sva);
InsertDataNode(doc, elemProduct, "Amount", amount);
return doc.OuterXml;
}
private static void InsertDataNode(XmlDocument doc, XmlElement parentElem, string nodeName, string nodeValue)
{
XmlElement elem = doc.CreateElement(nodeName);
elem.InnerText = nodeValue;
parentElem.AppendChild(elem);
}
and am getting the result as
<root>
<data>
<struct>
<PartnerCD>123</PartnerCD>
<UserName>api</UserName>
<Password>pass</Password>
<Action>token</Action>
<Token>4847898</Token>
<Products>
<Product>
<ProductID>123</ProductID>
<SVA>e8a8227c-bba3-4f32-a2cd-15e8f246344b</SVA>
<Amount>700</Amount>
</Product>
</Products>
</struct>
</data>
</root>
I want the PartnerCD and ProductId elements as integer and Amount element as decimal. I have tried XMLNodeType but no use.
Well the data type is irrelevant in the XML as such, it's all defined in the schema it uses, that's where the declaration of expected data types are stored.
So if you have a XSD that says that /root/data/struct/PartnerCD is of type xs:int then you will get validation errors upon validating the file. The XML itself is just a container of all data, it doesn't include the meta information. You CAN define it manually, but the Point is beyond me in about 99.99% of the cases, and it's more or less a bastard of XML, like MSXML that will understand what you're up to.
Xml has no concept of "data type" natively, the XmlNodeType you refer to is what type of Node it is (such as element, attribute etc), not what type of data is contained within it.
Personally I would create an object and Serialize\Deserialize to\from XML like so.....
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
namespace _37321906
{
class Program
{
static void Main(string[] args)
{
Root root = new Root();
root.Data.Struct.PartnerCD = 123;
root.Data.Struct.UserName = "api";
root.Data.Struct.Password = "pass";
root.Data.Struct.Action = "token";
root.Data.Struct.Token = 4847898;
root.Data.Struct.Products.Product.Add(new Product { ProductID = 123, SVA = "e8a8227c-bba3-4f32-a2cd-15e8f246344b", Amount = 700.0001 });
// Serialize the root object to XML
Serialize<Root>(root);
// Deserialize from XML
Root DeserializeRoot = Deserialize<Root>();
}
private static void Serialize<T>(T data)
{
// Use a file stream here.
using (TextWriter WriteFileStream = new StreamWriter("test.xml"))
{
// Construct a SoapFormatter and use it
// to serialize the data to the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Serialize EmployeeList to the file stream
SerializerObj.Serialize(WriteFileStream, data);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
}
private static T Deserialize<T>() where T : new()
{
//List<Employee> EmployeeList2 = new List<Employee>();
// Create an instance of T
T ReturnListOfT = CreateInstance<T>();
// Create a new file stream for reading the XML file
using (FileStream ReadFileStream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Construct a XmlSerializer and use it
// to serialize the data from the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Deserialize the hashtable from the file
ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
// return the Deserialized data.
return ReturnListOfT;
}
// function to create instance of T
public static T CreateInstance<T>() where T : new()
{
return (T)Activator.CreateInstance(typeof(T));
}
}
[XmlRoot(ElementName = "Product")]
public class Product
{
[XmlElement(ElementName = "ProductID")]
public int ProductID { get; set; }
[XmlElement(ElementName = "SVA")]
public string SVA { get; set; }
[XmlElement(ElementName = "Amount")]
public double Amount { get; set; }
}
[XmlRoot(ElementName = "Products")]
public class Products
{
public Products()
{
this.Product = new List<Product>();
}
[XmlElement(ElementName = "Product")]
public List<Product> Product { get; set; }
}
[XmlRoot(ElementName = "struct")]
public class Struct
{
public Struct()
{
this.Products = new Products();
}
[XmlElement(ElementName = "PartnerCD")]
public int PartnerCD { get; set; }
[XmlElement(ElementName = "UserName")]
public string UserName { get; set; }
[XmlElement(ElementName = "Password")]
public string Password { get; set; }
[XmlElement(ElementName = "Action")]
public string Action { get; set; }
[XmlElement(ElementName = "OfficeCD")]
public string OfficeCD { get; set; }
[XmlElement(ElementName = "ChannelCD")]
public string ChannelCD { get; set; }
[XmlElement(ElementName = "Token")]
public int Token { get; set; }
[XmlElement(ElementName = "Notes")]
public string Notes { get; set; }
[XmlElement(ElementName = "Products")]
public Products Products { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
public Data()
{
this.Struct = new Struct();
}
[XmlElement(ElementName = "struct")]
public Struct Struct { get; set; }
}
[XmlRoot(ElementName = "root")]
public class Root
{
public Root()
{
this.Data = new Data();
}
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
}
}
find your XML in the build folder called test.xml

Prefixes and Namespaces with C# Objects

I am attempting to create a POST function that serialises C# class objects into XML.
The part I am having great difficulty with is adding namespace prefixes to the sub-root element's children, so in this instance, contact children only.
The only way I seem to be able to get the prefix onto the child elements of contactis to add them through SerializerNamespace class, however I can only get this to attach to the root element, CreateContact.
How can I achieve this?
XML Currently Produced:
<?xml version=\"1.0\"?>
<CreateContact xmlns:a="http://foo.co.uk/Contact" xmlns="http://foo.co.uk">
<a:contact>
<a:Email>stest#gmail.com</a:Email>
<a:FirstName>Simon</a:FirstName>
<a:LastName>Test</a:LastName>
<a:Phone>09088408501</a:Phone>
<a:Title>Mr</a:Title>
</a:contact>
</CreateContact>
Serialisation function:
public static void CreateContact(Contact contact)
{
string tmp = url;
string xml = "";
string result = "";
XmlDocument xd = new XmlDocument();
var cc = new CreateContact();
cc.contact = contact;
var xs = new XmlSerializer(cc.GetType());
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("a", "http://foo.co.uk/Contact");
using (MemoryStream ms = new MemoryStream())
{
xs.Serialize(ms, cc, xsn);
ms.Position = 0;
xd.Load(ms);
xml = xd.InnerXml;
}
using (WebClient web = new WebClient())
{
web.Credentials = new NetworkCredential(username, password);
web.Headers.Add("Content-Type", "application/xml");
try
{
result = web.UploadString(tmp, "POST", xml);
}
catch (WebException ex)
{
}
}
}
XML Class Constructs:
[Serializable()]
[XmlRoot(ElementName = "CreateContact", Namespace = "http://foo.co.uk")]
public class CreateContact
{
[XmlElement(ElementName = "contact", Namespace = "http://foo.co.uk/Contact")]
public Contact contact { get; set; }
}
[DataContract(Name = "Contact", Namespace = "http://foo.co.uk/Contact")]
[XmlType("a")]
public class Contact
{
[XmlElement(ElementName = "Email", Namespace = "http://foo.co.uk/Contact")]
[DataMember(Name = "Email")]
public string Email { get; set; }
[XmlElement(ElementName = "FirstName", Namespace = "http://foo.co.uk/Contact")]
[DataMember(Name = "FirstName")]
public string Firstname { get; set; }
[XmlElement(ElementName = "LastName", Namespace = "http://foo.co.uk/Contact")]
[DataMember(Name = "LastName")]
public string Lastname { get; set; }
[XmlElement(ElementName = "Phone", Namespace = "http://foo.co.uk/Contact")]
[DataMember(Name = "Phone")]
public string Phone { get; set; }
[XmlElement(ElementName = "Title", Namespace = "http://foo.co.uk/Contact")]
[DataMember(Name = "Title")]
public string Title { get; set; }
}
XML Desired:
<?xml version=\"1.0\"?>
<CreateContact xmlns="http://foo.co.uk">
<contact xmlns:a="http://foo.co.uk/Contact">
<a:Email>stest#gmail.com</a:Email>
<a:FirstName>Simon</a:FirstName>
<a:LastName>Test</a:LastName>
<a:Phone>09088408501</a:Phone>
<a:Title>Mr</a:Title>
</contact>
</CreateContact>
As alluded in the comments, the reason for the difference is that contact should be in the namespace http://foo.co.uk, not http://foo.co.uk/Contact.
As an aside, a couple of further comments:
You probably don't need the DataMember attributes, unless you're using DataContractSerializer somewhere else.
Most of the Xml* attributes are superfluous here, and could be removed or consolidated by inheriting from XmlRoot.
If all you need is the XML string, you'd be better off serialising to something like a StringWriter rather than to a stream and then loading into the DOM just to get the text (see this question if you need the XML declaration to specify utf-8)
So, you'd get the XML as below:
var xsn = new XmlSerializerNamespaces();
xsn.Add("a", "http://foo.co.uk/Contact");
var xs = new XmlSerializer(typeof(CreateContact));
using (var stringWriter = new StringWriter())
{
xs.Serialize(stringWriter, cc, xsn);
xml = stringWriter.ToString();
}
With your classes defined as:
[XmlRoot(ElementName = "CreateContact", Namespace = "http://foo.co.uk")]
public class CreateContact
{
[XmlElement(ElementName = "contact")]
public Contact Contact { get; set; }
}
[XmlRoot("contact", Namespace = "http://foo.co.uk/Contact")]
public class Contact
{
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Title { get; set; }
}
See this fiddle for the complete example.

How to use protobuf-net for xml-serialization?

Maybe i misunderstood something for serialization. i wanna serialize my .net object fastest way. i made some googling i found protobuf. Myfirstquestion is ProtoBuf.Net has avility for xml serailization.if it has, can i use it for xml serialization.
My model:
[XmlType]
public class CT {
[XmlElement(Order = 1)]
public int Foo { get; set; }
}
[XmlType]
public class TE {
[XmlElement(Order = 1)]
public int Bar { get; set; }
}
[XmlType]
public class TD {
[XmlElement(Order = 1)]
public List CTs { get; set; }
[XmlElement(Order = 2)]
public List TEs { get; set; }
[XmlElement(Order = 3)]
public string Code { get; set; }
[XmlElement(Order = 4)]
public string Message { get; set; }
[XmlElement(Order = 5)]
public DateTime StartDate { get; set; }
[XmlElement(Order = 6)]
public DateTime EndDate { get; set; }
}
my serializer :
CT ct = new CT() { Foo = 1 };
List<CT> list = new List<CT>();
list.Add(ct);
TE te = new TE() { Bar = 2 };
List<TE> list2 = new List<TE>();
list2.Add(te);
TD td = new TD() { Code = "Test",Message = "Yusuf",StartDate = DateTime.Now,EndDate = DateTime.Now,CTs = list,TEs = list2 };
List<TD> list3 = new List<TD>();
list3.Add(td);
Stopwatch stopwatch5 = new Stopwatch();
stopwatch5.Start();
string str = String.Empty;
using (MemoryStream stream = new MemoryStream())
{
byte[] data = Serialize(list3);
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data); <--SHOULD CREATE XML
doc.LoadXml(xml);
// str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
}
stopwatch5.Stop();
Console.WriteLine(((double)(stopwatch5.Elapsed.TotalMilliseconds * 1000000) / 1000000).ToString("0.00 ns"));
Console.Read();
}
public static byte[] Serialize(List<TD> tData) {
using(var ms = new MemoryStream()) {
ProtoBuf.Serializer.Serialize(ms,tData);
return ms.ToArray();
}
}
public static List<TD> Deserialize(byte[] tData) {
using(var ms = new MemoryStream(tData)) {
return ProtoBuf.Serializer.Deserialize<List<TD>>(ms);
}
}
it should create xml as a result of " string xml = Encoding.UTF8.GetString(data);". But doesn't. How can i produxe xml result?
Protocol buffers doesn't serialize objects to XML.
It produces binary data. And it has its own set of attributes.
Check this answer
Is Protobuf-net's serialization/deserialization faster than XML ? Yes, by far.
However XmlSerializer is fast enough for most of the tasks.
What you should remind when using it though, is:
XmlSerializer instance is generating code on the fly and compile this code into an assembly.
This generated assembly is then used to serialize and deserialize your objects really fast.
So you should cache instances of XmlSerializer (and avoid recreating them)
you could add a warm up by calling Serialize and Deserialize in order to initialize inner objects and jit them.
You could even go further by generating the auto-generated assembly by yourself, but then you should remember to regenerate each time you change the objects (It can be automated with a MsBuild Task).
You can also look further optimizations:
On Stack Overflow
With Sgen
You can only have one tag at the root level of your xml. So either TD cannot be a list, or you must have an outer tag around the List. This code works
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TD td = new TD()
{
Code = "Test",
Message = "Yusuf",
StartDate = DateTime.Now,
EndDate = DateTime.Now,
CTs = new List<CT>() {
new CT() { Foo = 1},
new CT() { Foo = 2},
new CT() { Foo = 3},
},
TEs = new List<TE>() {
new TE() { Bar = 1},
new TE() { Bar = 2},
new TE() { Bar = 3},
}
};
using (MemoryStream stream = new MemoryStream())
{
byte[] data = Serialize(td);
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data);
doc.LoadXml(xml);
// str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
}
}
public static byte[] Serialize(TD tData)
{
using (var ms = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(TD));
serializer.Serialize(ms, tData);
return ms.ToArray();
}
}
public static TD Deserialize(byte[] tData)
{
using (var ms = new MemoryStream(tData))
{
XmlSerializer xs = new XmlSerializer(typeof(TD));
return (TD)xs.Deserialize(ms);
}
}
}
[XmlRoot("CT")]
public class CT
{
[XmlElement(ElementName = "Foo", Order = 1)]
public int Foo { get; set; }
}
[XmlRoot("TE")]
public class TE
{
[XmlElement(ElementName = "Bar", Order = 1)]
public int Bar { get; set; }
}
[XmlRoot("TD")]
public class TD
{
[XmlElement(ElementName = "CTs", Order = 1)]
public List<CT> CTs { get; set; }
[XmlElement(ElementName = "TEs", Order = 2)]
public List<TE> TEs { get; set; }
[XmlElement(ElementName = "Code", Order = 3)]
public string Code { get; set; }
[XmlElement(ElementName = "Message", Order = 4)]
public string Message { get; set; }
[XmlElement(ElementName = "StartDate", Order = 5)]
public DateTime StartDate { get; set; }
[XmlElement(ElementName = "EndDate", Order = 6)]
public DateTime EndDate { get; set; }
}
}
​

Categories