LINQ XML Read different hierarchies into 1 object - c#

I have an XML file
<searchResponse requestID=“500” status=“success”>
<pso>
<psoID ID=“770e8400-e29b-41d4-a716-446655448549”
targetID=“mezeoAccount”/>
<data>
<email>user2#example.net</email>
<quotaMeg>100</quotaMeg>
<quotaUsed>23</quotaUsed>
<realm>Mezeo</realm>
<path>/san1/</path>
<billing>user2</billing>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>
</data>
</pso>
</searchRequest>
and I want to extract the data into a single business object. Am I better to go
MezeoAccount mcspAccount = new MezeoAccount();
mcspAccount.PsoID = doc.Element("psoID").Attribute("ID").Value;
mcspAccount.Email = doc.Element("email").Value;
...
or build a list even though I know there is only 1 record in the file?
var psoQuery = from pso in doc.Descendants("data")
select new MezeoAccount {
PsoID = pso.Parent.Element("psoID").Attribute("ID").Value,
Email = pso.Element("email").Value,
... };
What would people suggest would be the more correct way, or a better way even, if I missed something.

If you know that your xml only will contain one record of the data in mind you shouldn't create a list for it. So your first example looks fine.
A pattern I personally use is something like this:
public class MezeoAccount
{
public string PsoID { get; set; }
public string Email { get; set; }
public static MezeoAccount CreateFromXml(XmlDocument xml)
{
return new MezeoAccount()
{
PsoID = xml.Element("psoID").Attribute("ID").Value,
Email = doc.Element("email").Value;
};
}
}
//Usage
var mezeoAccount = MezeoAccount.CreateFromXml(xml);

It looks like you didn't get a working answer to this question. Assuming that there can only be one account in the XML file, I would do it like this:
using System;
using System.Linq;
using System.Xml.Linq;
public class MezeoAccount
{
public string PsoId { get; set; }
public string Email { get; set; }
public int QuotaMeg { get; set; }
// Other properties...
}
public class Program
{
public static void Main()
{
XDocument doc = XDocument.Load("input.xml");
XElement pso = doc.Element("searchResponse").Element("pso");
XElement data = pso.Element("data");
MezeoAccount x = new MezeoAccount
{
PsoId = pso.Element("psoID").Attribute("ID").Value,
Email = data.Element("email").Value,
QuotaMeg = int.Parse(data.Element("quotaMeg").Value),
// Other properties...
};
}
}

Related

Populate a class with LINQ query

I need to populate a class using an XML file.
<Ship>
<Name>Base Ship</Name>
<Owner>PG</Owner>
<Aim>
<Type>base</Type>
<Value>10</Value>
<Last>-1</Last>
</Aim>
<Aim>
<Type>cannon</Type>
<Value>10</Value>
<Last>2</Last>
</Aim>
<Dodge>
<Type>base</Type>
<Value>10</Value>
<Last>-1</Last>
</Dodge>
<EmPower>
<Type>base</Type>
<Value>10</Value>
<Last>-1</Last>
</EmPower>
</Ship>
My problem is how to populate a Dictionary<string, CustomStruct>
This is the struct:
public struct Stat
{
public int StatValue { get; set; }
public int StatLast { get; set; }
public Stat(int statValue, int statLast)
{
StatValue = statValue;
StatLast = statLast;
}
}
My LINQ query looks like this:
string loadDataPath = Application.persistentDataPath + "/saveData.xml";
XDocument loadData = XDocument.Load(loadDataPath);
var query = from item in loadData.Elements("Ship")
select new Ship()
{
Name = (string) item.Element("Name"),
Owner = (string) item.Element("Owner"),
Aim = item.Elements("Aim") // <-- Here lies the problem.
// ...
};
For each Aim XElements I need to populate the Aim dictionary using the following method:
Aim TKey = XML Type
Aim TValue.StatValue = XML Value
Aim TValue.StatLast = XML Last
Use the ToDictionary() extension method to achieve what you want:
Aim = item.Elements("Aim").ToDictionary(x => (string)x.Element("Type"), x => new Stat(int.Parse((string)x.Element("Value")), int.Parse((string)x.Element("Last"))))
Also, I had to change struct to class for Stat, in order to make it work.
If you want to use struct you need to modify it a bit:
public struct Stat
{
public int StatValue { get; set; }
public int StatLast { get; set; }
public Stat(int statValue, int statLast) : this()
{
StatValue = statValue;
StatLast = statLast;
}
}
Got this from this answer.
Here is the correct LINQ to XML syntax:
Aim = item.Elements("Aim").ToDictionary(
e => (string)e.Element("Type"),
e => new Stat((int)e.Element("Value"), (int)e.Element("Last")))

C# GET REST xml to object deserialization encoding clash

I wanna get xml file from http and convert it to object.
So right now I have 2 methods: one to get http response body like that:
var httpClient = new HttpClient();
var op = httpClient.GetStringAsync(uri);
var httpResponseBody = "";
try {
var httpResponse = await httpClient.GetAsync(uri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
return httpResponseBody;
}
...
which returns string httpResponseBody.
Second one tries to convert this xml in string to object:
res = await task;
var reader = new XmlSerializer(typeof(Schedule));
using (var tr = new MemoryStream(Encoding.UTF8.GetBytes(res)))
{
var schedule = (Schedule)reader.Deserialize(tr);
return schedule;
}
The problem is that the content I receive is in different encoding and I don't know how to convert it to make deserialization possible.
I am getting something like this:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ramowka><dzien name=\"PoniedziaÅ\u0082ek\" count=\"2\"/></ramowka>\n
How to get rid of '\n' and Å\u0082 (should be ł) ?
Right now I am getting Exception from reader.Deserialize: {"<ramowka xmlns=''> was not expected."}
Schedule class:
[XmlType(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Schedule
{
[XmlElementAttribute("ramowka")]
public ScheduleDay[] AuditionDays { get; set; }
}
I've changed Schedule class to:
[XmlType(AnonymousType = true)]
[XmlRootAttribute("ramowka")]
public class Schedule
{
[XmlElementAttribute("dzien")]
public ScheduleDay[] AuditionDays { get; set; }
}
Now it looks like working. Thanks Petter for hint with Root attribute.
Setting the root object on the XmlSerializer fixes the problem:
var reader = new XmlSerializer(typeof(Schedule), new XmlRootAttribute("ramowka"));
...though I used slightly different attributes:
[DataContract]
public class ScheduleDay
{
[DataMember, XmlAttribute]
public string name { get; set; }
[DataMember, XmlAttribute]
public string count { get; set; }
}
[DataContract]
public class Schedule
{
[DataMember]
public ScheduleDay dzien { get; set; }
}
I haven't tried yours yet, but these work.
For a collection of ScheduleDays, this combo works:
[XmlType("dzien")]
public class ScheduleDay
{
[XmlAttribute]
public string name { get; set; }
[XmlAttribute]
public string count { get; set; }
}
Usage:
XmlSerializer reader = new XmlSerializer(typeof(List<ScheduleDay>), new XmlRootAttribute("ramowka"));
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(Xml)))
{
List<ScheduleDay> schedule = (List<ScheduleDay>)reader.Deserialize(stream);
}
The Schedule class just disappeared from the equation.
Escapes in the HTML
The \ns are part of the XML structure, so no need to worry about those. The deserializer will translate \u0082 into its equivalent character, which is
BREAK PERMITTED HERE. Which you probably don't want. The Å looks out of place too -- it's the last letter of the Norwegian alphabet and not used in Polish, AFAIK.

Issue de-serializing XML to Object - There is an error in XML document (0, 0)

I'm trying to read in element values from a simple XML doc and bind these to an object however I'm running into problems with my XML document. I have validated it and can confirm there are no issues with the document itself however expanding the results on the line:
var nodes = from xDoc in xml.Descendants("RewriteRule")
select xmlSerializer.Deserialize(xml.CreateReader()) as Url;
Show "There is an error in XML document (0, 0)"
The inner exceptions reads <RewriteRules xmlns=''> was not expected.
I'm not sure what I'm doing wrong here?
My XML is below:
<?xml version="1.0" encoding="utf-8" ?>
<RewriteRules>
<RewriteRule>
<From>fromurl</From>
<To>tourl</To>
<Type>301</Type>
</RewriteRule>
</RewriteRules>
The code that loads the XML file and attempts to de-serialize it: -
public static UrlCollection GetRewriteXML(string fileName)
{
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
var xmlSerializer = new XmlSerializer(typeof(Url));
var nodes = from xDoc in xml.Descendants("RewriteRule")
select xmlSerializer.Deserialize(xml.CreateReader()) as Url;
return nodes as UrlCollection;
}
My Url object class: -
[Serializable]
[XmlRoot("RewriteRule")]
public class Url
{
[XmlElement("From")]
public string From { get; set; }
[XmlElement("To")]
public string To { get; set; }
[XmlElement("Type")]
public string StatusCode { get; set; }
public Url()
{
}
public Url(Url url)
{
url.From = this.From;
url.To = this.To;
url.StatusCode = this.StatusCode;
}
}
Can anyone see what I'm doing wrong here?
Thanks
I'm not too familiar with the from select statement, but it seems you just pass in xml which is the whole XDocument, instead of the XElement that is your RewriteRule. That is why you get the error message that RewriteRules is unknown - the XmlSerializer expects a single RewriteRule.
I managed to rewrite your code using LINQ instead (but if you know how to get the single element from the from select statement, that should work equally well).
This should give you the correct result - rr is the XElement that is returned from Descendants:
public static IEnumerable<Url> GetRewriteXML()
{
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
var xmlSerializer = new XmlSerializer(typeof(Url));
var nodes = xml.Descendants("RewriteRule")
.Select(rr => xmlSerializer.Deserialize(rr.CreateReader()) as Url);
return nodes;
}
EDIT:
The name of your Url class does not match. You need to rename it to "RewriteRule"or define it this way:
[Serializable]
[System.Xml.Serialization.XmlRoot("RewriteRule")]
public class Url
{
[XmlElement("From")]
public string From { get; set; }
[XmlElement("To")]
public string To { get; set; }
[XmlElement("Type")]
public string StatusCode { get; set; }
public Url()
{
}
public Url(Url url)
{
url.From = this.From;
url.To = this.To;
url.StatusCode = this.StatusCode;
}
}
my approach is more used, when you deserialize it directly to an instance of a class.
If you want to use XDocument you may just write it like this. I do not use XDocument in my code. Since I need to deserialize the full xml packages I do it directly with deserialize of the root node. Therefore, I have proposed to the the root node to the right place in the previous post.
With XDocument you can access subparts directly. Here is working code for your purpose, but there may be others who can help you setting this code up more elegant:
public static UrlCollection GetRewriteXML(string fileName)
{
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
var urls = from s in xml.Descendants("RewriteRule")
select new
{
From = (string)s.Element("From").Value,
To = (string)s.Element("To").Value,
StatusCode = (string)s.Element("Type").Value
};
UrlCollection nodes = new UrlCollection();
foreach (var url in urls)
{
nodes.Add(new Url(url.From, url.To, url.StatusCode));
}
return nodes;
}
[Serializable]
public class Url
{
[XmlElement("From")]
public string From { get; set; }
[XmlElement("To")]
public string To { get; set; }
[XmlElement("Type")]
public string StatusCode { get; set; }
public Url()
{
}
public Url(string From, string To, string StatusCode)
{
this.From = From;
this.To = To;
this.StatusCode = StatusCode;
}
public Url(Url url)
{
url.From = this.From;
url.To = this.To;
url.StatusCode = this.StatusCode;
}
}

How to load XML Elements using LINQ from XDocument into a class (not using Descendants)

How to load XML Elements using LINQ from XDocument into a c# class. I don't want to use XDocument.Descendants because the settings are not repeated and occur only once in the XML.
This works but I think there must be another way where I don't have to use IEnumerable or use ToArray() and use the first element [0]. Any suggestions?
CODE
public class BackupItem // class needed so LINQ reads XML into an editable DbGrid
{
public bool Backup { get; set; }
public bool IncludeSubDir { get; set; }
public string BackupLabel { get; set; }
public string BackupPath { get; set; }
}
public class BackupSettings
{
public bool IncludeDateStamp { get; set; }
public bool DatePrefix { get; set; }
public bool DateSuffix { get; set; }
public string DateFormat { get; set; }
public bool ZipCompress { get; set; }
public bool ZipPrefix { get; set; }
public bool ZipSuffix { get; set; }
public string ZipText { get; set; }
public bool BackupToSiblingFolder { get; set; }
public string SiblingFolder { get; set; }
public bool BackupToFolder { get; set; }
public bool IncludeFullPath { get; set; }
public string BackupFolder { get; set; }
}
// use a LINQ query to load xml file into datagridview and make datagrid editable (create class with get/set)
var q = from arg in GvXMLDoc.Descendants("BackupItem")
select new BackupItem()
{
Backup = (bool)arg.Element("IncludeDirectory"),
IncludeSubDir = (bool)arg.Element("IncludeSubDirectories"),
BackupLabel = (string)arg.Element("BackupLabel"),
BackupPath = (string)arg.Element("BackupPath")
};
dataGridView1.DataSource = q.ToList();
// load global variable
IEnumerable<BackupSettings> GvBackupSettings = from arg in GvXMLDoc.Element("Backup").Elements("Settings")
select new BackupSettings()
{
IncludeDateStamp = (bool)arg.Element("IncludeDateStamp"),
DatePrefix = (bool)arg.Element("DatePrefix"),
DateSuffix = (bool)arg.Element("DateSuffix"),
DateFormat = (string)arg.Element("DateFormat"),
ZipCompress = (bool)arg.Element("ZipCompress"),
ZipPrefix = (bool)arg.Element("ZipPrefix"),
ZipSuffix = (bool)arg.Element("ZipSuffix"),
ZipText = (string)arg.Element("ZipText"),
BackupToSiblingFolder = (bool)arg.Element("BackupToSiblingFolder"),
SiblingFolder = (string)arg.Element("SiblingFolder"),
BackupToFolder = (bool)arg.Element("BackupToFolder"),
IncludeFullPath = (bool)arg.Element("IncludeFullPath"),
BackupFolder = (string)arg.Element("BackupFolder")
};
I can access the values but it seems a very 'messy way'.
var s = GvBackupSettings.ToArray()[0].DateSuffix;
var t = GvBackupSettings.ToArray()[0].DateFormat;
XML FILE
<?xml version='1.0'?>
<Backup>
<Settings>
<IncludeDateStamp>true</IncludeDateStamp>
<DatePrefix>false</DatePrefix>
<DateSuffix>true</DateSuffix>
<DateFormat>yyyy-MM-dd h.m.s</DateFormat>
<ZipCompress>false</ZipCompress>
<ZipPrefix>false</ZipPrefix>
<ZipSuffix>false</ZipSuffix>
<ZipText></ZipText>
<BackupToSiblingFolder>true</BackupToSiblingFolder>
<SiblingFolder>backups</SiblingFolder>
<BackupToFolder>false</BackupToFolder>
<IncludeFullPath>true</IncludeFullPath>
<BackupFolder>C:\\backup</BackupFolder>
</Settings>
<BackupItem>
<IncludeDirectory>true</IncludeDirectory>
<IncludeSubDirectories>true</IncludeSubDirectories>
<BackupLabel>Backup1</BackupLabel>
<BackupPath>C:\TestFiles\Xml\Samples</BackupPath>
</BackupItem>
<BackupItem>
<IncludeDirectory>true</IncludeDirectory>
<IncludeSubDirectories>false</IncludeSubDirectories>
<BackupLabel>Backup2</BackupLabel>
<BackupPath>C:\TestFiles\Xml\Samples</BackupPath>
</BackupItem>
</Backup>
EDIT 1
I also am trying to deserialize the XML (thanks for idea) so I don't have to cast each item. This does not work.. I don't want to have to run the XSD tool either and have a huge class file...
XmlRootAttribute rootAttribute = new XmlRootAttribute("Backup");
XmlSerializer deserializer = new XmlSerializer((typeof(BackupSettings)), rootAttribute);
GvBackupSettings = (BackupSettings)deserializer.Deserialize(XmlReader.Create(GvXMLFileName));
EDIT 2
Ended up serializing and deserializing xml as suggested below using stardard XSD.exe tool to generate the c# class. Especially as I had to read and write to same xml file. Note: Make sure you verify/modify the generated xsd file first.
You don't need a query if you just need the first element:
XElement settings = GvXMLDoc.Element("Backup").Element("Settings");
BackupSettings GvBackupSettings = new BackupSettings
{
IncludeDateStamp = (bool)settings.Element("IncludeDateStamp"),
DatePrefix = (bool)settings.Element("DatePrefix"),
...
};
var s = GvBackupSettings.DateSuffix;
var t = GvBackupSettings.DateFormat;

How to loop through a set of XElements?

http://www.dreamincode.net/forums/xml.php?showuser=335389
Given the XML above, how can I iterate through each element inside of the 'lastvisitors' element, given that each child group is the same with just different values?
//Load latest visitors.
var visitorXML = xml.Element("ipb").Element("profile").Element("latestvisitors");
So now I have captured the XElement containing everything I need. Is there a way to loop through the elements to get what I need?
I have this POCO object called Visitor whose only purpose is to hold the necesary information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharpDIC.Entities
{
public class Visitor
{
public string ID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Photo { get; set; }
public string Visited { get; set; }
}
}
Thanks again for the help.
You can probably just do something like this in Linq:
XDocument xml = XDocument.Parse(xmlString);
var visitors = (from visitor in xml.Descendants("latestvisitors").Descendants("user")
select new Visitor() {
ID = visitor.Element("id").Value,
Name = visitor.Element("name").Value,
Url = visitor.Element("url").Value,
Photo = visitor.Element("photo").Value,
Visited = visitor.Element("visited").Value
});
The only caveat here is that I didn't do any null checking.
Just do a linq query to select all the elements as your object.
var visitors = (from v in xml.Element("ipb").Element("profile")
.Element("latestvisitors").Elements()
select new Visitor {
ID = (string)v.Element("id"),
Name = (string)v.Element("name"),
}).ToList();

Categories