I have an XML like this:
<XmlSports CreateDate="2022-12-04T17:47:53.5569879Z">
<Sport Name="eSports" ID="2357">
<Event Name="NBA2K, NBA Blitz League" ID="66838" IsLive="false" CategoryID="9357">
<Match Name="LA Clippers (THA KID) Esports - Milwaukee Bucks (SHARPSHOOTER)" ID="2711992" StartDate="2022-12-04T17:36:00" MatchType="Live">
<Bet Name="Money Line" ID="43859970" IsLive="true">
<Odd Name="1" ID="297613016" Value="2.26"/>
<Odd Name="2" ID="297613021" Value="1.58"/>
</Bet>
<Bet Name="Spread" ID="43859969" IsLive="true">
<Odd Name="1" ID="297614398" Value="1.83" SpecialBetValue="2.5"/>
<Odd Name="2" ID="297614399" Value="1.90" SpecialBetValue="2.5"/>
</Bet>
<Bet Name="Total" ID="43859971" IsLive="true">
<Odd Name="Over" ID="297613741" Value="1.86" SpecialBetValue="140.5"/>
<Odd Name="Under" ID="297613740" Value="1.86" SpecialBetValue="140.5"/>
</Bet>
</Match>
</Event>
<Event Name="FIFA, GT League" ID="62647" IsLive="false" CategoryID="8212">
.
. **and so on, the content is too long to post it here**
.
</Sport>
</XmlSports>
and the goal is to deserialize it into C# classes.
My classes are as such:
using System.Xml.Serialization;
namespace BettingDataAPI.Models
{
[Serializable, XmlRoot("Sport")]
public class Sport
{
[XmlElement("ID")]
public int ID { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Event")]
public List<Event> Events { get; set; }
}
}
also:
using System.Xml.Serialization;
namespace BettingDataAPI.Models
{
[XmlType("Event")]
public class Event
{
[XmlElement("ID")]
public int ID { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("CategoryID")]
public int CategoryID { get; set; }
[XmlElement("IsLive")]
public bool IsLive { get; set; }
[XmlElement("Match")]
public List<Match> Matches { get; set; }
}
}
I also have the classes: Odd, Match and Bet
This is my code:
private static async Task<Sport> DeserializeXMLToObject()
{
private static readonly string xmlUrl = #"*here is the url*";
XmlSerializer ser = new XmlSerializer(typeof(Sport));
//WebClient client = new WebClient();
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(xmlUrl).Result;
string xml = "";
if (response.IsSuccessStatusCode)
{
xml = response.Content.ReadAsStringAsync().Result;
}
Sport sport = new Sport();
using(TextReader reader = new StringReader(xml))
{
sport = (Sport)ser.Deserialize(reader);
}
return sport;
}
but on this line
sport = (Sport)ser.Deserialize(reader);
it gives me the following error:
InnerException = {"<XmlSports xmlns=''> was not expected."}
I think it is because I've had created a class XmlSports but I removed it because when I run code first migration with Entity Framework, it said that there was an error because it couldn't create a primary key for the table XmlSports, so I removed this class and made the Sport class the root class instead.
I tried to create an xsd file from this xml (I created a test.xml file and copied and pasted the content from the url) but it didn't happen because it said that: There was an error processing 'test.xml'.
I don't have experience with xml content and serialization and deserialization in general so it's pretty hard for me to understand what's wrong and how to fix it.
Any suggestions would be highly appreciated!
Thank you!
This should get you started. If you need more help ask
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(XmlSports));
XmlSports sports = (XmlSports)serializer.Deserialize(reader);
}
}
public class XmlSports
{
public Sport Sport { get; set; }
}
public class Sport
{
[XmlAttribute("ID")]
public int ID { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlElement("Event")]
public List<Event> Events { get; set; }
}
public class Event
{
[XmlAttribute("ID")]
public int ID { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("CategoryID")]
public int CategoryID { get; set; }
[XmlAttribute("IsLive")]
public bool IsLive { get; set; }
[XmlElement("Match")]
public List<Match> Matches { get; set; }
}
public class Match
{ }
}
Related
Parsing XML file using SSIS / C#
operations like fetching record count from trailer , TIN from body and store into a variable or somewhere temporarily(your suggestions please) for further processing. I don't want to store it in a table.
Please find the sample xml mentioned below
<ACOParticipantData xmlns:xsi="">
<Header>
<HeaderCode>HDR_PFPRVDR</HeaderCode>
<FileCreationDate>20160101</FileCreationDate>
<ACOProgCode>21</ACOProgCode>
</Header>
<Participants>
<Participant>
<ACO_ID>V199</ACO_ID>
<TIN>123456789</TIN>
<Old_TIN>987654321</Old_TIN>
<Org_NPI>1234567890</Org_NPI>
<Ind_NPI>1234567890</Ind_NPI>
<CCN>123456</CCN>
<PRG_Eff_Dt>20160101</PRG_Eff_Dt>
<PRG_Term_Dt>20161231</PRG_Term_Dt>
</Participant>
</Participants>
<Trailer>
<TrailerCode>TRL_PFPRVDR</TrailerCode>
<FileCreationDate>20160101</FileCreationDate>
<RecordCount>1</RecordCount>
</Trailer>
</ACOParticipantData>
You need to get fist get list of Participants then fetch all Participants tin number into list like
Here i created console app for your demonstration purpose.
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load(#"Path to your xml file");
List<long> tinList = new List<long>();
tinList = doc.Descendants("Participants").Elements().Elements("TIN").Select(x => (long)x).ToList();
foreach (long tin in tinList)
{
Console.WriteLine(tin);
}
Console.ReadLine();
}
}
Output: (For 2 Participants)
You need to create a class for each node and use XML deserialisation to create the object.
I had to remove the empty namespace as the deserialisation process requires a valid namespace.
Also you can change the type of the properties according to your needs.
using System;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
public class Program
{
public class ACOParticipantData
{
public Header Header { get; set; }
public Participant[] Participants { get; set; }
}
public class Header
{
public string HeaderCode { get; set; }
public string FileCreationDate { get; set; }
public string ACOProgCode { get; set; }
}
public class Participant
{
public string ACO_ID { get; set; }
public string TIN { get; set; }
public string Old_TIN { get; set; }
public string Org_NPI { get; set; }
public string Ind_NPI { get; set; }
public string CCN { get; set; }
public string PRG_Eff_Dt { get; set; }
public string PRG_Term_Dt { get; set; }
}
public class Trailer
{
public string TrailerCode { get; set; }
public string FileCreationDate { get; set; }
public string RecordCount { get; set; }
}
public static void Main()
{
var xmlString = #"<ACOParticipantData>
<Header>
<HeaderCode>HDR_PFPRVDR</HeaderCode>
<FileCreationDate>20160101</FileCreationDate>
<ACOProgCode>21</ACOProgCode>
</Header>
<Participants>
<Participant>
<ACO_ID>V199</ACO_ID>
<TIN>123456789</TIN>
<Old_TIN>987654321</Old_TIN>
<Org_NPI>1234567890</Org_NPI>
<Ind_NPI>1234567890</Ind_NPI>
<CCN>123456</CCN>
<PRG_Eff_Dt>20160101</PRG_Eff_Dt>
<PRG_Term_Dt>20161231</PRG_Term_Dt>
</Participant>
<Participant>
<ACO_ID>V199</ACO_ID>
<TIN>123456780</TIN>
<Old_TIN>987654321</Old_TIN>
<Org_NPI>1234567890</Org_NPI>
<Ind_NPI>1234567890</Ind_NPI>
<CCN>123456</CCN>
<PRG_Eff_Dt>20160101</PRG_Eff_Dt>
<PRG_Term_Dt>20161231</PRG_Term_Dt>
</Participant>
</Participants>
<Trailer>
<TrailerCode>TRL_PFPRVDR</TrailerCode>
<FileCreationDate>20160101</FileCreationDate>
<RecordCount>1</RecordCount>
</Trailer>
</ACOParticipantData>";
var serializer = new XmlSerializer(typeof(ACOParticipantData));
ACOParticipantData obj = null;
using (var reader = new StringReader(xmlString))
{
obj = (ACOParticipantData)serializer.Deserialize(reader);
}
if (obj == null)
{
return;
}
foreach (var tin in obj.Participants.Select(x => x.TIN))
{
Console.WriteLine(tin);
}
}
}
Output:
123456789
123456780
I have the following XML;
<?xml version="1.0" encoding="UTF-8" ?>
<feedback>
<report_metadata>
<org_name>example.com</org_name>
</report_metadata>
</feedback>
and the following Feedback.cs class;
[XmlRoot("feedback", Namespace = "", IsNullable = false)]
public class Feedback
{
[XmlElement("report_metadata")]
public MetaData MetaData { get; set; }
}
[XmlType("report_metadata")]
public class MetaData
{
[XmlAttribute("org_name")]
public string Organisation { get; set; }
}
When I attempt to deserialize, the value for Organisation is null.
var xml = System.IO.File.ReadAllText("example.xml");
var serializer = new XmlSerializer(typeof(Feedback));
using (var reader = new StringReader(input))
{
var feedback = (Feedback)serializer.Deserialize(reader);
}
Yet, when I change Feedback.cs to the following, it works (obviously the property name has changed).
[XmlType("report_metadata")]
public class MetaData
{
//[XmlAttribute("org_name")]
public string org_name { get; set; }
}
I want the property to be Organisation, not org_name.
In the example XML file org_name is an XML element, not an XML attribute. Changing
[XmlAttribute("org_name")] to [XmlElement("org_name")] at the Organisation property will deserialize it as an element:
[XmlElement("org_name")]
public string Organisation { get; set; }
probably just typo
[XmlAttribute("org_name")]
public string Organisation { get; set; }
was supposed to be
[XmlElement("org_name")]
public string Organisation { get; set; }
Try to modify your Xml classes like
[XmlRoot(ElementName = "report_metadata")]
public class MetaData
{
[XmlElement(ElementName = "org_name")]
public string Organisation { get; set; }
}
[XmlRoot(ElementName = "feedback")]
public class Feedback
{
[XmlElement(ElementName = "report_metadata")]
public MetaData MetaData { get; set; }
}
Then you will get your desired output like
class Program
{
static void Main(string[] args)
{
Feedback feedback = new Feedback();
var xml = System.IO.File.ReadAllText(#"C:\Users\Nullplex6\source\repos\ConsoleApp4\ConsoleApp4\Files\XMLFile1.xml");
var serializer = new XmlSerializer(typeof(Feedback));
using (var reader = new StringReader(xml))
{
feedback = (Feedback)serializer.Deserialize(reader);
}
Console.WriteLine($"Organization: {feedback.MetaData.Organisation}");
Console.ReadLine();
}
}
Output:
I know there are several posts out there with this topic, but I can't seem to figure out what is the problem here. I have serialized and deserialized xml several times, and never had this error.
The exception message is:
There is an error in XML document (1, 2).
With InnerException:
<InvoiceChangeRequest xmlns=''> was not expected.
XML file I want to deserialize:
<ns1:InvoiceChangeRequest xmlns:ns1="http://kmd.dk/fie/external_invoiceDistribution">
<CONTROL_FIELDS>
<STRUCTURID>0000000001</STRUCTURID>
<OPERA>GET</OPERA>
<WIID>000050371220</WIID>
</CONTROL_FIELDS>
<HEADER_IN>
<MANDT>751</MANDT>
<BELNR>1234567890</BELNR>
</HEADER_IN>
<ITEMS>
<ITEM_FIELDS_IN>
<BUZEI>001</BUZEI>
<BUKRS>0020</BUKRS>
</ITEM_FIELDS_IN>
</ITEMS>
</ns1:InvoiceChangeRequest>
Class I'm trying to deserialize to:
[XmlRoot(Namespace = "http://kmd.dk/fie/external_invoiceDistribution", IsNullable = false)]
public class InvoiceChangeRequest
{
[XmlElement("CONTROL_FIELDS")] public ControlFields Styrefelter;
[XmlElement("HEADER_IN")] public HeaderIn HeaderfelterInd;
[XmlElement("ITEMS")] public Items Linjer;
}
public class HeaderIn
{
[XmlElement("MANDT")] public string Kommunenummer;
[XmlElement("BELNR")] public string RegnskabsbilagsNummer;
}
public class Items
{
[XmlElement("ITEM_FIELDS_IN")] public Itemfield[] ItemfelterInd;
}
public class Itemfield
{
[XmlElement("BUZEI")] public string Linjenummer;
[XmlElement("BUKRS")] public string Firmakode;
}
Deserialization code:
XmlSerializer serializer = new XmlSerializer(typeof(InvoiceChangeRequest));
var request = serializer.Deserialize(new StringReader(output)) as InvoiceChangeRequest;
In your XML file your root element is the namespace http://kmd.dk/fie/external_invoiceDistribution with prefix ns1.
The element <CONTROL_FIELDS> isn't because it isn't prefixed. Your serialization class doesn't take this into account though. That means that it expects that <CONTROL_FIELDS> and the other elements are ALSO in the ns1 namespace.
To get the serializer parse the elements correctly add the Namespace to the elements, setting it to an empty string:
[XmlRoot(Namespace = "http://kmd.dk/fie/external_invoiceDistribution", IsNullable = false)]
public class InvoiceChangeRequest
{
[XmlElement("CONTROL_FIELDS", Namespace = "")]
public ControlFields Styrefelter { get; set; }
[XmlElement("HEADER_IN", Namespace = "")]
public HeaderIn HeaderfelterInd { get; set; }
[XmlElement("ITEMS", Namespace = "")]
public Items Linjer { get; set; }
}
This will de-serialize the given XML as intended.
In case of de-serialization issues I often create the classes in memory and then serialize that so I can inspect the resulting XML. That often gives clues on what is missing or being added compared to the input document:
var ms = new MemoryStream();
serializer.Serialize(ms, new InvoiceChangeRequest {
Styrefelter = new ControlFields { Opera="test"}
});
var s = Encoding.UTF8.GetString(ms.ToArray());
And then inspect s for differences.
You can replace 'ns1:' with string.Empty.
Below classes should serialize.
public class Item
{
[XmlElement("BUZEI")]
public string Buzei { get; set; }
[XmlElement("BUKRS")]
public string Bukrs { get; set; }
}
public class Header
{
[XmlElement("MANDT")]
public string Mandt { get; set; }
[XmlElement("BELNR")]
public string Belnr { get; set; }
}
public class ControlFields
{
[XmlElement("STRUCTURID")]
public string StructuredId { get; set; }
[XmlElement("OPERA")]
public string Opera { get; set; }
[XmlElement("WIID")]
public string Wild { get; set; }
}
public class InvoiceChangeRequest
{
[XmlElement("CONTROL_FIELDS")]
public ControlFields ControlFields { get; set; }
[XmlElement("HEADER_IN")]
public Header Header { get; set; }
[XmlArray("ITEMS")]
[XmlArrayItem("ITEM_FIELDS_IN")]
public List<Item> Items { get; set; }
}
I'm trying to deserialize the following xml into an Object.
Xml got multiple namespaces.
I tried to deserialize the Xml into an object.
The object (data) has a reference to the LastChannel Object.
But when i ask for data.channel which should give me the LastChannel, i get a nullpointer.
Xml:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:mp="http://www.tagesschau.de/rss/1.0/modules/metaplus/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>title</title>
<description>Default description</description>
<dc:date>2013-04-15 13:27:06</dc:date>
<sy:updateBase>2013-04-15 13:27:06</sy:updateBase>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>12</sy:updateFrequency>
</channel>
</rdf:RDF>
The objects look like this:
[XmlRoot("RDF", Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#")]
public class LastRss
{
[XmlElement("channel")]
public LastChannel channel { get; set; }
}
and
public class LastChannel
{
[XmlElement("title")]
public string title { get; set; }
[XmlElement("description")]
public string description { get; set; }
[XmlElement("date", Namespace = "http://purl.org/dc/elements/1.1/")]
public DateTime date { get; set; }
[XmlElement("updateBase", Namespace = "http://purl.org/rss/1.0/modules/syndication/")]
public DateTime updateBase { get; set; }
[XmlElement("updatePeriod", Namespace = "http://purl.org/rss/1.0/modules/syndication/")]
public string updatePeriod { get; set; }
[XmlElement("updateFrequency", Namespace = "http://purl.org/rss/1.0/modules/syndication/")]
public int updateFrequency { get; set; }
}
Anybody sees why the data.channel ist null?
Serializer:
LastRss data = new LastRss();
XmlSerializer serializer = new XmlSerializer(typeof(LastRss));
System.IO.TextReader reader = new System.IO.StringReader(xml);
try
{
object o = serializer.Deserialize(reader);
data = (LastRss)o;
}
Your channel is in the default xmlns, viz http://purl.org/rss/1.0/
[XmlElement("channel", Namespace = "http://purl.org/rss/1.0/")]
public LastChannel channel { get; set; }
You'll also need to correct the date formats e.g. 2013-04-15**T**13:27:06
Here's my issue : I need to get a list of resources from a web services, and deserialize it into object. But it doesn't work, despite the facts my code worked with another xml file. So I can't figure why it doesn't work, and I'm stuck with that !
Here's the XML :
<ResourceDataSet xmlns="http://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/">
<Resources>
<RES_UID>blabla</RES_UID>
<RES_NAME>blabla</RES_NAME>
<RES_CODE>blabla</RES_CODE>
<RES_GROUP>blabla</RES_GROUP>
<RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
<Resources>
<RES_UID>blabla</RES_UID>
<RES_NAME>blabla</RES_NAME>
<RES_CODE>blabla</RES_CODE>
<RES_GROUP>blabla</RES_GROUP>
<RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
<Resources>
<RES_UID>blabla</RES_UID>
<RES_NAME>blabla</RES_NAME>
<RES_CODE>blabla</RES_CODE>
<RES_GROUP>blabla</RES_GROUP>
<RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
</ResourceDataSet>
The class I want to deserialize into :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Threading.Tasks;
using System.Collections;
namespace TestWPF
{
[Serializable()]
public class Employee
{
[System.Xml.Serialization.XmlElement("RES_UID")]
public int RES_UID { get; set; }
[System.Xml.Serialization.XmlElement("RES_NAME")]
public String RES_NAME { get; set; }
[System.Xml.Serialization.XmlElement("RES_CODE")]
public String RES_CODE { get; set; }
[System.Xml.Serialization.XmlElement("RES_GROUP")]
public String RES_GROUP { get; set; }
[System.Xml.Serialization.XmlElement("RES_COST_CENTER")]
public String RES_COST_CENTER { get; set; }
public Employee()
{ }
public Employee(int r_id, String res_name, String res_code, String res_group, String res_cost_center)
{
this.RES_UID = r_id;
this.RES_NAME = res_name;
this.RES_CODE = res_code;
this.RES_GROUP = res_group;
this.RES_COST_CENTER = res_cost_center;
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("ResourceDataSet")]
public class EmployeeList //: IEnumerator, IEnumerable
{
public EmployeeList() {Items = new List<Employee>();}
[XmlArray("ResourceDataSet")]
[XmlArrayItem("Resources")]
public List<Employee> Items {get;set;}
}
}
And the code I use to deserialize :
EmployeeList lstEmployee = null;
XmlSerializer xs = new XmlSerializer(typeof(ServersList));
StreamReader sr = new StreamReader("testEmployee.xml");
lstEmployee = (EmployeeList)serializer.Deserialize(sr);
reader.Close();
for (int i = 0; i < lstEmployee.Items.Count(); i++)
{
MessageBox.Show(lstEmployee.Items[i].RES_NAME);
}
And when I try to launch I receive this error message :
Firstly your xml file is invalid - RES_UID is expecting an int, so even when you get your serialization working you'll run into that problem.
You're also not taking into account the namespace. The following class works:
[Serializable()]
public class Employee
{
[System.Xml.Serialization.XmlElement("RES_UID")]
public int RES_UID { get; set; }
[System.Xml.Serialization.XmlElement("RES_NAME")]
public String RES_NAME { get; set; }
[System.Xml.Serialization.XmlElement("RES_CODE")]
public String RES_CODE { get; set; }
[System.Xml.Serialization.XmlElement("RES_GROUP")]
public String RES_GROUP { get; set; }
[System.Xml.Serialization.XmlElement("RES_COST_CENTER")]
public String RES_COST_CENTER { get; set; }
public Employee()
{ }
public Employee(int r_id, String res_name, String res_code, String res_group, String res_cost_center)
{
this.RES_UID = r_id;
this.RES_NAME = res_name;
this.RES_CODE = res_code;
this.RES_GROUP = res_group;
this.RES_COST_CENTER = res_cost_center;
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("ResourceDataSet", Namespace = "http://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/")]
public class EmployeeList //: IEnumerator, IEnumerable
{
public EmployeeList() {Items = new List<Employee>();}
[XmlElement("Resources", Type = typeof(Employee))]
public List<Employee> Items {get;set;}
}
}
and your calling code with the typos fixed:
EmployeeList lstEmployee = null;
XmlSerializer xs = new XmlSerializer(typeof(EmployeeList));
StreamReader sr = new StreamReader("testEmployee.xml");
lstEmployee = (EmployeeList)xs.Deserialize(sr);
sr.Close();
for (int i = 0; i < lstEmployee.Items.Count(); i++)
{
MessageBox.Show(lstEmployee.Items[i].RES_NAME);
}
Remember to fix your xml to be ints otherwise it still won't work
You need to either decorate your root entity with the XmlRoot attribute or Or specify the root attribute when de serializing at runtime.
Here is a thread about this issue
https://stackoverflow.com/a/1557145/1305119