'Parsing XML file in .NET
I want to get from XML file one or list of records from https://dchrs.com.pl/wp-content/themes/Lucid/doc/notowania.xml
Shortly XSD schema looks like:
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="notowania">
<xs:complexType>
<xs:sequence>
<xs:element name="data_wygenerowania" type="xs:string" />
<xs:element name="kurs_euro" type="xs:decimal" />
<xs:element name="kategorie">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="kategoria">
<xs:complexType>
<xs:sequence>
<xs:element name="nazwa" type="xs:string" />
<xs:element name="pozycje">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" name="pozycja">
<xs:complexType>
<xs:sequence>
<xs:element name="nazwa" type="xs:string" />
<xs:element name="jednostka" type="xs:string" />
<xs:element name="cena_pln" type="xs:string" />
<xs:element name="cena_euro" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I have created a few classes like:
- Flower:
using System.Xml.Serialization;
[XmlRoot(ElementName = "pozycja")]
public class Flower
{
[XmlElement(ElementName = "nazwa")]
public string Name { get; set; }
[XmlElement(ElementName = "jednostka")]
public string Unit { get; set; }
[XmlElement(ElementName = "cena_pln")]
public string PricePln { get; set; }
[XmlElement(ElementName = "cena_euro")]
public string PriceEuro { get; set; }
}
- Flowers:
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot(ElementName = "pozycje")]
public class Flowers
{
[XmlElement(ElementName = "nazwa")]
public List<string> Flower { get; set; }
}
- TypeOfFlower:
using System.Xml.Serialization;
[XmlRoot(ElementName = "kategoria")]
public class TypeOfFlower
{
[XmlElement(ElementName = "nazwa")]
public string Category { get; set; }
[XmlElement(ElementName = "pozycje")]
public Flowers Flowers { get; set; }
}
- and
FlowerConnectorto fetch them from Url:
using RestSharp;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using XmlSerializer = RestSharp.Serializers.XmlSerializer;
namespace FlowerShop.ApplicationServices.Components.Flowers
{
public class FlowersConnector : IFlowersConnector
{
private readonly RestClient restClient;
private readonly string baseUrl = "https://dchrs.com.pl/";
string getUrl = "https://dchrs.com.pl/wp-content/themes/Lucid/doc/notowania.xml";
public FlowersConnector()
{
this.restClient = new RestClient(baseUrl);
}
public Task<Flowers> Fetch(string name)
{
var request = new RestRequest("wp-content/themes/Lucid/doc/notowania.xml", Method.GET);
var queryResult = restClient.ExecuteAsync(request);
var flowersXml = ; // ???
}
}
}
After a few trials I got to give up.
Can somebody help me out with solving this problem?
[Edit] Here's what I've tried:
using RestSharp.Serialization;
using RestSharp.Serializers;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using XmlSerializer = RestSharp.Serializers.XmlSerializer;
namespace FlowerShop.ApplicationServices.Components.Flowers
{
public class FlowersConnector : IFlowersConnector
{
private readonly RestClient restClient;
private readonly string baseUrl = "https://dchrs.com.pl/";
string getUrl = "https://dchrs.com.pl/wp-content/themes/Lucid/doc/notowania.xml";
public FlowersConnector()
{
this.restClient = new RestClient(baseUrl);
}
//public async Task<Flowers> Fetch(string name)
//{
// var request = new RestRequest("wp-content/themes/Lucid/doc/notowania.xml", Method.GET);
// var queryResult = await restClient.ExecuteAsync(request);
//}
public Task<Flowers> Fetch(string name)
{
var request = new RestRequest("wp-content/themes/Lucid/doc/notowania.xml", Method.GET);
var queryResult = restClient.ExecuteAsync(request);
var flowersXml = XmlConvert<Flowers>.ListDeserializeObject(queryResult);
return new Flowers();
}
}
}
//////private readonly string apiUrl = "https://dchrs.com.pl/wp-content/themes/Lucid/doc/notowania.xml";
public class XmlConvert<T> where T : new()
{
//Serialized objects become XML strings
public static string SerializeObject(T myObj)
{
var xmlStr = string.Empty;
if (myObj != null)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (var stringWriter = new StringWriter())
{
xs.Serialize(stringWriter, myObj);
xmlStr = stringWriter + "";
}
}
return xmlStr;
}
//Serialized objects become XML strings
public static string SerializeObject1(T Object)
{
var xmlStr = string.Empty;
if (Object != null)
{
XmlSerializer xs = new XmlSerializer(typeof(T));//Initializing XMLSerializer object
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
//writer.Formatting = Formatting.None;// Do not apply special format settings. This is the default value.
writer.Formatting = Formatting.Indented; //Set indent.
xs.Serialize(writer, Object);
stream.Position = 0; // Get or set the current location in the stream. Must be set, otherwise the default last position, the data in the stream is not received
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
xmlStr += line;
}
}
writer.Close();
}
return xmlStr;
}
internal static object ListDeserializeObject(RestSharp.IRestResponse queryResult)
{
throw new System.NotImplementedException();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
