XML Deserialize returning 0 results in c# - c#

hoping someone can point out what I'm missing here. I'm trying to deserialize the result of a SOAP API call.
I have the following InnerXML within the soapenv:Body of the result
<ns2:OperationResponseInfo xmlns:ns2="http://www.XXXXX.com/webservice/">
<status>
<statusCode>0</statusCode>
<statusDesc>No Error</statusDesc>
</status>
<result>
<record>
<param>
<name>totalrecord</name>
<value>9</value>
</param>
</record>
</result>
<result>
<record>
<param>
<name>ALARMID</name>
<value>1581807719208</value>
</param>
<param>
<name>ALARMDESC</name>
<value>xxxxxxxxxxxxxx</value>
</param>
<param>
<name>ALARMSTATUS</name>
<value>Unacknowledged</value>
</param>
</record>
</result>
</ns2:OperationResponseInfo>
I am trying to deserialize to the following objects :-
[XmlRoot(ElementName = "OperationResponseInfo", Namespace = "http://www.XXXXX.com/webservice/")]
public class OperationResponseInfo
{
[XmlElement(ElementName = "status")]
public Status Status { get; set; }
[XmlElement(ElementName = "result")]
public List<Result> Result { get; set; }
//[XmlAttribute(AttributeName = "ns2", Namespace = "http://www.XXXXX.com/webservice/")]
//public string Ns2 { get; set; }
}
[XmlRoot(ElementName = "status")]
public class Status
{
[XmlElement(ElementName = "statusCode")]
public string StatusCode { get; set; }
[XmlElement(ElementName = "statusDesc")]
public string StatusDesc { get; set; }
}
[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "record")]
public List<Record> Record { get; set; }
}
[XmlRoot(ElementName = "record")]
public class Record
{
[XmlElement(ElementName = "param")]
public List<Param> Param { get; set; }
}
[XmlRoot(ElementName = "param")]
public class Param
{
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "value")]
public string Value { get; set; }
}
This is the deserialize method I am using
public class Serializer
{
public T Deserialize<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
and this is the call to the Deserialize method
var alarmList = ser.Deserialize<OperationResponseInfo>(docInner.InnerXml);
I have this working on another system (with different SOAP result format/c# classes etc.) but for some reason the Deserialize is returning 0 results. The code desn't error at all. What am I missing ?

Namespaces and inheritance rules between xmlns=..., namespace aliases, and what XmlSerializer assumes; try:
[XmlElement(ElementName = "status", Namespace = "")]
public Status Status { get; set; }
[XmlElement(ElementName = "result", Namespace = "")]
public List<Result> Result { get; set; }
More specifically:
when using xmlns="...", namespaces are inherited by descendants
when using ns2:blah, that prefix is not inherited by descendants - it must be explicit
so <status> and <result> are in the empty namespace
but XmlSerializer assumes that namespaces are inherited by default, so [XmlElement(ElementName = "status")] means "an element called status in the same namespace as the current context, i.e. http://www.XXXXX.com/webservice/"
so you need to explicitly tell XmlSerializer to go back to the empty namespace

Related

.net core deserialize xml response

I have this xml response as a stream from USPS 3.0 zip code look up api
<ZipCodeLookupResponse>
<Address ID="1">
<Address1>Unit 2</Address1>
<Address2>8 WILDWOOD DR</Address2>
<City>OLD LYME</City>
<State>CT</State>
<Zip5>06371</Zip5>
</Address>
<Address ID="2">
<Address1>Unit 2</Address1>
<Address2>8 WILDWOOD DR</Address2>
<City>OLD LYME</City>
<State>CT</State>
<Zip5>06371</Zip5>
</Address>
</ZipCodeLookupResponse>
and I'm trying to deserialize the response in an array of addresses.
[XmlRoot(ElementName = "ZipCodeLookupResponse")]
public class UspsAddress
{
[XmlArray("Address")]
public UspsAddressResult[] Addresses {get;set;}
}
[XmlRoot(ElementName = "Address")]
public class UspsAddressResult
{
[XmlElement(ElementName = "Address2")]
public string Adress1 { get; set; }
[XmlElement(ElementName = "Address1")]
public string Adress2 { get; set; }
[XmlElement(ElementName = "City")]
public string City { get; set; }
[XmlElement(ElementName = "State")]
public string State { get; set; }
[XmlElement(ElementName = "Zip5")]
public string Zip { get; set; }
}
This code is always returning an empty array. How can I fix this code to get both address from the response?
...
var content = await res.Content.ReadAsStreamAsync();
var serializer = new XmlSerializer(typeof(UspsAddress));
var results = (UspsAddress)serializer.Deserialize(content);
Instead of using XmlArray or XmlArrayItem, you can use the `XmlElement``
attribute to set the name of the child elements. The deserializer will recognize that there are multiple elements that are supposed to be deserialized into a list of objects.
Your class then looks like this:
[XmlRoot(ElementName = "ZipCodeLookupResponse")]
public class UspsAddress
{
[XmlElement("Address")]
public UspsAddressResult[] Addresses {get;set;}
}

C# deserialize an XML with two elements of same name but different attributes

I've been given an xml containing two elements that have the same namem, 'Log', but where each 'Log' element has different attribute names. E.g. the first 'Log' element has attribute name 'AttrA' and the second 'Log' element has 'AttrB'. I've tried using XmlArrayItem, but don't get it to read in the array. In the code below, the 'myObjectWithoutLog' object contains Elm, but not the Log data. Anybody know how I can get the two Log elements into the serialized object?
XML
<Elm DbLocation="C:\Data\Db\My.db">
<Log AttrA="C:\Data\Log\Log1.csv"/>
<Log AttrA="C:\Data\Log\Log2.csv"/>
</Elm>
C#
[XmlRoot(ElementName = "Elm", Namespace = "")]
public class myXmlObject
{
public myXmlObject()
{
}
[XmlAttribute("DbLocation", Namespace = "")]
public string DbLocation { get; set; }
[XmlArrayItem("Log", Namespace = "")]
public List<Log> logs { get; set; }
}
[XmlRoot(ElementName = "Log", Namespace = "")]
public class Log
{
[XmlAttribute("AttrA", Namespace = "")]
public string attrA { get; set; }
[XmlAttribute("AttrB", Namespace = "")]
public string attrB { get; set; }
}
public class RunIt
{
XmlSerializer serializer = new XmlSerializer(typeof(myXmlObject));
using (FileStream stream = new FileStream(myXmlFile, FileMode.Open))
{
myXmlObject myObjectWithoutLog = (myXmlObject)serializer.Deserialize(stream);
}
}
You're using wrong Xml attribute. You need to use XmlElement instead of XmlArrayItem.
[XmlRoot(ElementName = "Elm", Namespace = "")]
public class myXmlObject
{
public myXmlObject()
{
}
[XmlAttribute("DbLocation", Namespace = "")]
public string DbLocation { get; set; }
[XmlElement("Log", Namespace = "")]
public List<Log> logs { get; set; }
}
[XmlRoot(ElementName = "Log", Namespace = "")]
public class Log
{
[XmlAttribute("AttrA", Namespace = "")]
public string attrA { get; set; }
[XmlAttribute("AttrB", Namespace = "")]
public string attrB { get; set; }
}

Unable to get values from XML using xPath C#

I have two XML files, I am able to get data through xPath in c# from first XML which here:
<CONSOLIDATED_LIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://www.example/acb.xsd"
dateGenerated="2018-04-19T19:18:41.129-04:00">
<INDIVIDUALS>
<INDIVIDUAL>
<DATAID>2975591</DATAID>
<VERSIONNUM>1</VERSIONNUM>
<FIRST_NAME>ABC</FIRST_NAME>
<SECOND_NAME>XYZ</SECOND_NAME>
<INDIVIDUAL_ALIAS>
<QUALITY>Good</QUALITY>
<ALIAS_NAME>abcd</ALIAS_NAME>
</INDIVIDUAL_ALIAS>
<INDIVIDUAL_ALIAS>
<QUALITY>Bad</QUALITY>
<ALIAS_NAME>ylmn</ALIAS_NAME>
</INDIVIDUAL_ALIAS>
</INDIVIDUAL>
</INDIVIDUALS>
</CONSOLIDATED_LIST>
the way I am getting Data, please also see the comments inside code.
var xml = DownloadString(link); //link refers to XML file on web
e = XElement.Parse(xml);
//parentNodeForPerson = "INDIVIDUAL" for above and "sdnEntry" for below;
var lstIndividuals = e.Descendants(parentNodeForPerson);
Data _individualData;
List<Data> individualList = new List<Data>();
foreach (var individual in lstIndividuals)
{
_individualData = new Data();
//personParentValue1 for above is FIRST_NAME and below is firstName
_individualData.First_Name = individual.Descendants(personParentValue1)
.Any() == true ? individual.Descendants(personParentValue1).First().Value : "";
//personParentValue2 for above is SECOND_NAME and below is lastName
_individualData.Last_Name = individual.Descendants(personParentValue2)
.Any() == true ? individual.Descendants(personParentValue2).First().Value : "";
//childNodeForPersonfor above is INDIVIDUAL_ALIAS and below is aka
var lstIndvidualAlias = individual.Descendants(childNodeForPerson);
_individualData.Alias = new List<string>();
foreach (var alias in lstIndvidualAlias)
{
//personChildValue1 for above is ALIAS_NAME and below is lastName & fisrtName
if (!String.IsNullOrWhiteSpace(alias.Descendants(personChildValue1).First().Value))
{
_individualData.Alias.Add(alias.Descendants(personChildValue1)
.Any() == true ? alias.Descendants(personChildValue1).First().Value : "");
}
}
individualList.Add(_individualData);
}
this is XML where I am not getting data
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/List.xsd">
<publshInformation>
<Publish_Date>04/18/2018</Publish_Date>
<Record_Count>6299</Record_Count>
</publshInformation>
<sdnEntry>
<lastName>ABCD</lastName>
<fisrtName>XYS</fisrtName>
<akaList>
<aka>
<category>strong</category>
<lastName>ABCDT</lastName>
<fisrtName>XYS</fisrtName>
</aka>
<aka>
<category>Weak</category>
<lastName>AssDT</lastName>
<fisrtName>XYsS</fisrtName>
</aka>
</akaList>
</sdnEntry>
</List>
Edit:
Data Class
public class Data
{
public string Last_Name { get; set; }
public string First_Name { get; set; }
public List<string> Alias { get; set; }
}
SOLUTION
Yes, that's normal and depends on namespaces.
Just edit your LINQ queries and go from the one you used:
var lstIndividuals = e.Descendants(parentNodeForPerson);
to this:
var lstIndividuals = e.Descendants().Where(f => f.Name.LocalName.ToString() == parentNodeForPerson);
And it will work.
LONG EXPLANATION
The above solution basically ignores all namespace information when retrieving elements by name.
The things is that in the first XML you have:
<CONSOLIDATED_LIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://www.example/acb.xsd"
dateGenerated="2018-04-19T19:18:41.129-04:00">
Whilst in the second we find
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/List.xsd">
The second XML has two namespaces defined, which throws your LINQ queries out of whack; in fact, if you just removed this from the second XML
xmlns="http://tempuri.org/List.xsd"
your code would work without any changes.
To start with, you can read the answers to What does “xmlns” in XML mean?, especially this one. In short, namespaces can be useful e.g. when you combine XMLs from different sources and you get elements with the same name: by prepending a namespace, elements with the same name will be seen as different:
<namespaceA:elementName>blah </namespaceA:elementName>
<namespaceB:elementName>blah blah</namespaceB:elementName>
In your XML you have two namespaces but, when looking for Descendants you don't specify which namespace your elements belong to.
Another solution, perhaps more robust than the quick one above, would be specifying the namespace the name of an element belongs to, e.g. like the OP does in this question, using XName.Get: in your case, you would get the nodes of the second XML this way:
var lstIndividuals = e.Descendants(XName.Get(parentNodeForPerson, "http://tempuri.org/List.xsd"));
which works because the URI specified (http://tempuri.org/List.xsd) is the one of the default namespace you declared:
xmlns="http://tempuri.org/List.xsd"
and, since your elements don't have any prefix, it's the namespace your elements belong to; if they belonged to the other namespace you defined, i.e. xsi, their names would be e.g. xsi:sdnEntry.
Of course, since there's no default namespace in your first XML declaration but only the xsi namespace (which refers to a different URI), you would need to pass its URI to XName.Get and adapt your code.
In other words, you should make the URL parametric and pass it to your method, rather than hardcoding it like I did.
I tried your xml(s). I have used xml deserializer and it seems to work fine at my end. Please check the following code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using XML1;
using XML2;
namespace ConsoleApplication1
{
public class Program
{
private static void Main(string[] args)
{
var list1 = Deserialize<CONSOLIDATED_LIST>(#"CONSOLIDATED_LIST.xml"); // pass the path to your xml here
var list2 = Deserialize<List>(#"LIST.xml");
}
public static T Deserialize<T>(string path)
{
T obj;
XmlSerializer serializer = new XmlSerializer(typeof(T));
var reader = new StreamReader(path);
obj = (T)serializer.Deserialize(reader);
reader.Close();
return obj;
}
}
}
namespace XML1
{
[XmlRoot(ElementName = "INDIVIDUAL_ALIAS")]
public class INDIVIDUAL_ALIAS
{
[XmlElement(ElementName = "QUALITY")]
public string QUALITY { get; set; }
[XmlElement(ElementName = "ALIAS_NAME")]
public string ALIAS_NAME { get; set; }
}
[XmlRoot(ElementName = "INDIVIDUAL")]
public class INDIVIDUAL
{
[XmlElement(ElementName = "DATAID")]
public string DATAID { get; set; }
[XmlElement(ElementName = "VERSIONNUM")]
public string VERSIONNUM { get; set; }
[XmlElement(ElementName = "FIRST_NAME")]
public string FIRST_NAME { get; set; }
[XmlElement(ElementName = "SECOND_NAME")]
public string SECOND_NAME { get; set; }
[XmlElement(ElementName = "INDIVIDUAL_ALIAS")]
public List<INDIVIDUAL_ALIAS> INDIVIDUAL_ALIAS { get; set; }
}
[XmlRoot(ElementName = "INDIVIDUALS")]
public class INDIVIDUALS
{
[XmlElement(ElementName = "INDIVIDUAL")]
public INDIVIDUAL INDIVIDUAL { get; set; }
}
[XmlRoot(ElementName = "CONSOLIDATED_LIST")]
public class CONSOLIDATED_LIST
{
[XmlElement(ElementName = "INDIVIDUALS")]
public INDIVIDUALS INDIVIDUALS { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "noNamespaceSchemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string NoNamespaceSchemaLocation { get; set; }
[XmlAttribute(AttributeName = "dateGenerated")]
public string DateGenerated { get; set; }
}
}
namespace XML2
{
[XmlRoot(ElementName = "publshInformation", Namespace = "http://tempuri.org/List.xsd")]
public class PublshInformation
{
[XmlElement(ElementName = "Publish_Date", Namespace = "http://tempuri.org/List.xsd")]
public string Publish_Date { get; set; }
[XmlElement(ElementName = "Record_Count", Namespace = "http://tempuri.org/List.xsd")]
public string Record_Count { get; set; }
}
[XmlRoot(ElementName = "aka", Namespace = "http://tempuri.org/List.xsd")]
public class Aka
{
[XmlElement(ElementName = "category", Namespace = "http://tempuri.org/List.xsd")]
public string Category { get; set; }
[XmlElement(ElementName = "lastName", Namespace = "http://tempuri.org/List.xsd")]
public string LastName { get; set; }
[XmlElement(ElementName = "fisrtName", Namespace = "http://tempuri.org/List.xsd")]
public string FisrtName { get; set; }
}
[XmlRoot(ElementName = "akaList", Namespace = "http://tempuri.org/List.xsd")]
public class AkaList
{
[XmlElement(ElementName = "aka", Namespace = "http://tempuri.org/List.xsd")]
public List<Aka> Aka { get; set; }
}
[XmlRoot(ElementName = "sdnEntry", Namespace = "http://tempuri.org/List.xsd")]
public class SdnEntry
{
[XmlElement(ElementName = "lastName", Namespace = "http://tempuri.org/List.xsd")]
public string LastName { get; set; }
[XmlElement(ElementName = "fisrtName", Namespace = "http://tempuri.org/List.xsd")]
public string FisrtName { get; set; }
[XmlElement(ElementName = "akaList", Namespace = "http://tempuri.org/List.xsd")]
public AkaList AkaList { get; set; }
}
[XmlRoot(ElementName = "List", Namespace = "http://tempuri.org/List.xsd")]
public class List
{
[XmlElement(ElementName = "publshInformation", Namespace = "http://tempuri.org/List.xsd")]
public PublshInformation PublshInformation { get; set; }
[XmlElement(ElementName = "sdnEntry", Namespace = "http://tempuri.org/List.xsd")]
public SdnEntry SdnEntry { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}
}
I personally would prefer the xml deserializer. It would be easy and more maintainable. Your underlying code, would remain the same and the deserialization can be done base on the Type T in your xml

How to Deserialize an xml element with only attributes in C#?

I am using System.Xml.Serialization.XmlSerializer class.I need to Deserialize the following XML in C#:
<message from="abc" to="xyz" xml:lang="en" id="Vx4Ix-14" type="chat">
<received xmlns="urn:xmpp:receipts" id="9beea4d7-aa1e-4f3c-929c-712b56164b63"/>
</message>
Following is my Class to deserialize it :
[XmlRoot(ElementName = "message")]
public class Message
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "from")]
public string From { get; set; }
[XmlAttribute(AttributeName = "to")]
public string To { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "xml:lang")]
public string Language { get; set; }
[XmlElement(ElementName = "received", Namespace = "urn:xmpp:receipts")]
public Received Received { get; set; }
}
public class Received
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
The "received" xml element has only attributes and I want te deserialize that element to get "id" value of that element.
But when I use the above class to Deserialize , I get all the values except "id" attribute value of "received" xml element. I get the value of Received property as null.
Please let me know what is wrong with my class?
This is my Deserializer Method:
public static T Deserialize<T>(string xml)
{
T deserializedObject = default(T);
try
{
var serializer = new XmlSerializer(typeof(T));
using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
deserializedObject = (T)serializer.Deserialize(memoryStream);
}
return deserializedObject;
}
catch (Exception)
{
return deserializedObject;
}
}
Your class looks good and also works for me. Maybe you're not using the deserializer correctly!? I successfully tried it with your XML like this:
var serializer = new XmlSerializer(typeof(Message));
var message = (Message)serializer.Deserialize(
new FileStream(#"C:\Users\homann.k\Desktop\test.xml", FileMode.Open, FileAccess.Read));

Need help to deserialize XML data

I am trying to deserialize the below XML using C#
<?xml version="1.0" encoding="utf-8"?>
<Invoice>
<Samples>
<Sample>
<AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
<AccountNumber>55761598808</AccountNumber>
</Sample>
<Sample>
<AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
<AccountNumber>55761598808</AccountNumber>
</Sample>
</Samples>
</Invoice>
Here are the classes that I have defined to deserialize
[DataContract(Name = "Sample")]
public class Sample
{
[DataMember(Name = "AccountId")]
public string AccountId { get; set; }
[DataMember(Name = "AccountNumber")]
public string AccountNumber { get; set; }
}
[DataContract(Name = "Samples")]
public class Samples
{
[DataMember(Name = "Sample")]
public List<Sample> Sample { get; set; }
}
[DataContract(Name = "Invoice")]
public class Invoice
{
[DataMember(Name = "Samples")]
public Samples Samples { get; set; }
}
The corresponding test case to deserialize is as follows
public void SampleXmlTest()
{
dynamic env = SUT.GetEnvironment();
string dbConnStrUrjanet = (string)env.AvidUtility.UrjanetDB;
XmlSerializer deserializer = new XmlSerializer(typeof(CommonAvidXmlDto.Invoice));
TextReader reader = new StreamReader(#"C:\Users\SJuluru\Desktop\Sample XML\Samplexml.xml");
Object obj = deserializer.Deserialize(reader);
CommonAvidXmlDto.Invoice XmlData4 = (CommonAvidXmlDto.Invoice)obj;
After running the test case debug mode, XmlData4 has null without having the XML data. Kindly help me how to modify this code to work.
DataContract and DataMember are attribute of DataContractSerializer so you shoud replace your serializer msdn or use XmlSerializer's attributes (XmlRoot, XmlElement etc)
Change deserialize classes:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace ConsoleApp4
{
[XmlRoot(ElementName = "Sample")]
public class Sample
{
[XmlElement(ElementName = "AccountId")]
public string AccountId { get; set; }
[XmlElement(ElementName = "AccountNumber")]
public string AccountNumber { get; set; }
}
[XmlRoot(ElementName = "Samples")]
public class Samples
{
[XmlElement(ElementName = "Sample")]
public List<Sample> Sample { get; set; }
}
[XmlRoot(ElementName = "Invoice")]
public class Invoice
{
[XmlElement(ElementName = "Samples")]
public Samples Samples { get; set; }
}
}
Use below method for deserialize:
private static Invoice LoadInvoice(string fileName)
{
var serializer = new XmlSerializer(typeof(Invoice));
if (!File.Exists(fileName))
{
return null;
}
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return (Invoice)serializer.Deserialize(fs);
}
}
Result:

Categories