Linq to XML query analyz - c#

This query does work but I am not sure it is proper way to write this kind of query. I feel it is using too many Descendants and Parent.
Is there a better way to write this query?
There can be more than one catalog in XML.
static IEnumerable<Parts> GetAllParts(XDocument doc, string catalog, string groupId, string subGroupId)
{
var parts = (from p in doc.Descendants("ROOT").Descendants("CATALOG").Descendants("GROUP").Descendants("SUBGROUP").Descendants("BOM").Descendants("PARTS")
where (string)p.Parent.Parent.Parent.Parent.Element("IDENT").Value == catalog
&& p.Parent.Parent.Parent.Element("IDENT").Value == groupId
&& p.Parent.Parent.Element("IDENT").Value == subGroupId
select new Parts
{
ObjectId = int.Parse(p.Attribute("OBJECTID").Value),
Ident = p.Element("IDENT").Value,
List = p.Element("LIST").Value,
Descr = p.Element("DESC").Value
});
return parts;
}
}
public class Parts
{
public int ObjectId { get; set;}
public string Descr { get; set; }
public string Ident { get; set; }
public string List { get; set; }
}
Update: XML added.
<ROOT>
<CATALOG>
<OBJECT_ID>001</OBJECT_ID>
<OBJECT_IDENT>X001</OBJECT_IDENT>
<GROUP>
<OBJECT_ID>1001</OBJECT_ID>
<OBJECT_IDENT>01</OBJECT_IDENT>
<NAME>HOUSING</NAME>
<SUBGROUP>
<OBJECT_ID>5001</OBJECT_ID>
<OBJECT_IDENT>01.05</OBJECT_IDENT>
<NAME>DESIGN GROUP 1</NAME>
<BOM>
<OBJECT_ID>6001</OBJECT_ID>
<OBJECT_IDENT>010471</OBJECT_IDENT>
<PARTS>
<OBJECT_ID>2316673</OBJECT_ID>
<OBJECT_IDENT>A002010660</OBJECT_IDENT>
<DESC>SHORT BLOCK</DESC>
<NOTES>
<ROW>
<NOTES>Note 1</NOTES>
<BOM>010471</BOM>
<POS>1</POS>
</ROW>
<ROW>
<NOTES>Note 2</NOTES>
<BOM>010471</BOM>
<POS>2</POS>
</ROW>
</NOTES>
</PARTS>
<PARTS>
</PARTS>
<PARTS>
</PARTS>
</BOM>
</SUBGROUP>
<SUBGROUP>
</SUBGROUP>
<SUBGROUP>
</SUBGROUP>
</GROUP>
<GROUP>
</GROUP>
</CATALOG>
</ROOT>

Some suggestions regarding your question (I hope you'll improve your future posts) :
Your code and the XML posted doesn't work. For instance, the XML has <OBJECT_IDENT> element, but your LINQ has IDENT. Please craft it carefully and make sure it does work to avoid confusion.
Please put some effort in explaining the problem and giving clarification. "I am retrieving Parts data as function name says" is not clear enough as simply getting <Parts> elements doesn't need filtering but your LINQ has where .... clause.
This question seems to suits better in https://codereview.stackexchange.com/
And here is some suggestions regarding your code :
Use .Elements() to get direct child of current node as opposed to Descendants() which get all descendat nodes.
You can use Elements().Where() to filter the element so you can avoid traversing Parents
You can cast XElement to string/int to avoid exception in case such element not found
Example code snippet :
var parts = (from p in doc.Root
.Elements("CATALOG").Where(o => catalog == (string)o.Element("OBJECT_IDENT"))
.Elements("GROUP").Where(o => groupId == (string)o.Element("OBJECT_IDENT"))
.Elements("SUBGROUP").Where(o => subGroupId == (string)o.Element("OBJECT_IDENT"))
.Elements("BOM")
.Elements("PARTS")
select new Parts
{
ObjectId = (int)p.Element("OBJECT_ID"),
Ident = (string)p.Element("OBJECT_IDENT"),
List = (int)p.Element("LIST"),
Descr = (string)p.Element("DESC")
});

Related

Searching Xml Document with linq doesn't return anything

I'm trying to learn how to save and extract data with XML in C#, and even though I've read various answers to similar questions on here, I cannot get a hold of why my statement isn't returning anything.
The program I'm writing is just a test, in which I've saved data about a couple movies in the xml document and now I'm trying to retrieve some of them based on their cost.
This is the class that I've written for searching:
class ExtractData
{
private XDocument _xdoc;
public List<string> SearchByCost(double cost)
{
_xdoc = XDocument.Load(FileLocation.XmlFileLocation);
List<string> list = new List<string>();
var movies = from movie in _xdoc.Root.Elements("Name")
where Convert.ToDouble(movie.Element("Cost").Value) < cost
select movie;
foreach (var item in movies)
{
list.Add(item.Value);
}
return list;
}
}
This is how I'm trying to make it print it in the console:
eData = new ExtractData();
foreach (var movie in eData.SearchByCost(9))
{
Console.WriteLine(movie);
}
And this is the content of the XML document:
<?xml version="1.0" encoding="utf-8"?>
<Movies>
<Movie>
<Id>1</Id>
<Name>Shawshank Redemption</Name>
<Director>Frank Darabont</Director>
<Year>1994</Year>
<Cost>9.95</Cost>
</Movie>
<Movie>
<Id>2</Id>
<Name>Pulp Fiction</Name>
<Director>Quentin Tarantino</Director>
<Year>1995</Year>
<Cost>8.95</Cost>
</Movie>
<Movie>
<Id>3</Id>
<Name>Sharknado</Name>
<Director>Anthony Ferrante</Director>
<Year>2013</Year>
<Cost>5.95</Cost>
</Movie>
</Movies>
I hope this is enough information to try and help me out, and thanks in advance! :)
Root element contains Movie elements:
var movies = from movie in _xdoc.Root.Elements("Movie") // here instead of "Name"
where (double)movie.Element("Cost") < cost
select movie;
Also, XElement supports explicit casting to double. And you can replace the second loop with LINQ query (assume you want to select names of movies):
List<string> list = movies.Select(m => (string)m.Element("Name")).ToList();
In plain English, you seem to be trying to find movies cheaper than 9 (of some arbitrary currency).
You can write this as:
public IReadOnlyCollection<string> SearchByCost(XDocument xdoc, double cost)
{
return xdoc.Root.Elements("Movie")
.Where(movie => (double)movie.Element("Cost") < cost)
.Select(movie => movie.Element("Name").Value)
.ToList();
}

Linq to XML casting XML file to custom object

I'm using Linq to XML to read in an XML file and as part of this I'd like to create an object. My object looks like this:
public class Address
{
public string AccountRef { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
// more stuff here
}
And my XML file looks like this:
<rows>
<row>
<FIELD NAME="AccountRef">1234</FIELD>
<FIELD NAME="AddressLine1">My Address Line 1</FIELD>
<FIELD NAME="AddressLine2">My Address Line 2</FIELD>
</row>
<row>
<FIELD NAME="AccountRef">5678</FIELD>
<FIELD NAME="AddressLine1">My Address Line 3</FIELD>
<FIELD NAME="AddressLine2">My Address Line 4</FIELD>
</row>
</rows>
In terms of code, I've tried various things, but at present I have the following which returns the correct number of rows in the format:
<row><FIELD NAME="AccountRef">1234</FIELD><FIELD>...rest of data</row>
<row><FIELD NAME="AccountRef">5678</FIELD><FIELD>...rest of data</row>
The code that does this is:
var results = (from d in document.Descendants("row")
select d).ToList();
So basically what I'm trying to do is something like:
var results = (from d in document.Descendants("row")
select new Address
{
AccountRef = d.Attribute("AccountRef").Value,
AddressLine1 = d.Attribute("AddressLine1").Value
}).ToList();
Obviously because my nodes are the same (FIELD NAME) that won't work, so does anyone have an idea how I can achieve this?
you need to retrive field names and values before creating objects
var results = document.Descendants("row")
.Select(row=>row.Elements("FIELD").ToDictionary(x=>x.Attribute("NAME").Value, x=>x.Value))
.Select(d=>new Address
{
AccountRef = d["AccountRef"],
AddressLine1 = d["AddressLine1"],
AddressLine2 = d["AddressLine2"],
});
check demo

Different kind of deserializing XML in C#

I am trying to deserialize XML from AdWords API to a list of models. This is the XML format:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<report>
<report-name name='GeoPerformance Report for 5/26/2014 12:00:00 AM'/>
<date-range date='May 26, 2014'/>
<table>
<columns>
<column name='clicks' display='Clicks'/>
<column name='countryTerritory' display='Country/Territory'/>
<column name='day' display='Day'/>
</columns>
<row clicks='24286' countryTerritory='United States' day='2014-05-26'/>
<row clicks='26' countryTerritory='Africa' day='2014-05-26'/>
<row clicks='286' countryTerritory='Europe' day='2014-05-26'/>
<row clicks='242' countryTerritory='Asia' day='2014-05-26'/>
</table>
</report>
My model class is this:
[XmlRoot("report")]
public class AdWordsGeoPerformance
{
public int Clicks { get; set; }
public string CountryTerritory { get; set; }
public DateTime Day { get; set; }
}
I would like each row's attributes (clicks, countryTerritory, day) to map to an instance of AdWordsGeoPerformance.
Any suggestions on how I can do this?
You can try to generate the classes with the xsd tool:
xsd your.xsd /classes
In a fast search, I found this:
https://adwords.google.com/api/adwords/reportdownload/v201109/reportDefinition.xsd
I'm not sure if that's the correct schema, if not find it (it should be stated in the documentation) and use it to generate your classes.
I tend to favor Linq to XML over deserialization. You would have to play some games with many objects to be able to serialize/deserialize that data. Just using XDocument is a lot easier for me.
This might be helpful in you are looking for an alternate way:
var adWords = new List<AdWordsGeoPerformance>();
var xDoc = XDocument.Parse(doc);
foreach(var r in xDoc.Descendants("row"))
{
var adWord = new AdWordsGeoPerformance()
{
Clicks = int.Parse((string)r.Attribute("clicks")),
CountryTerritory = (string)r.Attribute("countryTerritory"),
Day = DateTime.Parse((string)r.Attribute("day")),
};
adWords.Add(adWord);
}

How to Search in XML and show results in GridView in C#?

I have an XML data of Employees, I want to parse this data and show it in Gridview using C# in ASP.NET.
Based on search parameters like Employee ID or Company ID or Department ID, I should be able to filter the data and update the GridView.
Checked few links in internet, but nothing matches this particular format.. Is it possible to achieve.. (Any links to) code will be helpful.
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Id> TG18-2002</Id>
<Name> AAPM^Test^Patterns</Name>
<Sex> O </Sex>
<Company>
<Id> 2.16</Id>
<Department>
<Id> 2.16.124</Id>
<Project>
<Id> 2.16.124.113543</Id>
</Project>
</Department>
</Company>
</Employee>
<Employee>
<ID> TG18-2003</ID>
<Name> AAPM^Test^Patt</Name>
<Sex> O </Sex>
<Company>
<ID> 2.16</ID>
<Department>
<ID> 2.16.124</ID>
<Project>
<ID> 2.16.124.113543</ID>
</Project>
</Department>
</Company>
</Employee>
<Employee>
</Employees>
Note: I am trying to build something like, this
Looking at the link you referenced, I think you're best off creating an Employee class and filling this with the XML data.
This will allow you to de-couple your data (in this case XML, but could be anything) from your view (in this case ASP.NET Web Forms, but again could be anything), which I find handy and seems to be common these days (MVVM etc..).
Another benefit is that you can turn a nested data source like XML into something a little flatter to make your view binding simpler, for example in the example you provided you could make the Company fields available as properties of the outer Employee class.
Yet another benefit is that if/when your view becomes responsive, then you can make properties of your view model observable, and therefore allow updates to the model, immediately update your view.
All that said, here is a small snippet showing your Employee class, and filling it from the XML sample you provided. I am using Linq to XML, but there are so many ways you could do this (adapters, readers, navigators...).
I think the important thing here is that you de-couple your data from the view.
class Employee
{
public string Id { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public Company Company { get; set; }
}
class Company
{
public string Id { get; set; }
public Department Department { get; set; }
}
class Department
{
public string Id { get; set; }
public Project Project { get; set; }
}
class Project
{
public string Id { get; set; }
}
var xml = #"<?xml version='1.0' encoding='utf-8'?>
<Employees>
<Employee>
<Id> TG18-2002</Id>
<Name> AAPM^Test^Patterns</Name>
<Sex> O </Sex>
<Company>
<Id> 2.16</Id>
<Department>
<Id> 2.16.124</Id>
<Project>
<Id> 2.16.124.113543</Id>
</Project>
</Department>
</Company>
</Employee>
<Employee>
<Id> TG18-2003</Id>
<Name> AAPM^Test^Patt</Name>
<Sex> O </Sex>
<Company>
<Id> 2.16</Id>
<Department>
<Id> 2.16.124</Id>
<Project>
<Id> 2.16.124.113543</Id>
</Project>
</Department>
</Company>
</Employee>
</Employees>
";
// read the xml into the class
var doc = XDocument.Parse(xml);
var data = (from row in doc.Root.Elements("Employee")
let company = row.Element("Company")
let dept = company.Element("Department")
let project = dept.Element("Project")
select new Employee
{
Id = row.Element("Id").Value.Trim(),
Name = row.Element("Name").Value.Trim(),
Sex = row.Element("Sex").Value.Trim(),
Company = new Company
{
Id = company.Element("Id").Value.Trim(),
Department = new Department
{
Id = dept.Element("Id").Value.Trim(),
Project = new Project
{
Id = project.Element("Id").Value.Trim()
}
}
}
});
// now you have a collection of 'employees', bind them..
Once we're that far, you need to decide how you want to query your data. The answer to this is as usual, it depends, and in this case I think it depends mostly on the size of the XML data you have and if it makes sense to bring this into memory or not.
If you can bring it into memory, then a collection of Employees is nice and simple to query using Linq and I would recommend this approach.
If the XML is large, then you will probably need to use an XMLReader to build your class. A little more complex, but the result should still be that you bind your grid to your class and keep the XML separate from the view.
DataSet xmlData = new DataSet();
xmlData.ReadXml(YourXMLPath);
GridviewControl1.DataSource = xmlData.Tables[0];
as far my understanding you need to do maipulication in data before showing it in Grid Control.
for that i recommend you to use LINQ query. you can manipulicate the things at client side with queries.
below i trying to solve the prob.
DataSet xmlData = new DataSet();
xmlData.ReadXml(YourXMLPath);
DataTable dt =From x in xmlData.Tables[0].AsEnumerable().Where(x=>x.Field<string>("Name").StartWith("A")) Selct(x=>x).CopytodDataTable;
now you can use "dt" for your data grid
xmlData.Tables[0]= dt;
GridviewControl1.DataSource = xmlData.Tables[0];
GridviewControl1.DataBind();//this line required if it is for asp.net
Hope it will helps you. :)

LINQ to XML via C#

I'm new to LINQ. I understand it's purpose. But I can't quite figure it out. I have an XML set that looks like the following:
<Results>
<Result>
<ID>1</ID>
<Name>John Smith</Name>
<EmailAddress>john#example.com</EmailAddress>
</Result>
<Result>
<ID>2</ID>
<Name>Bill Young</Name>
<EmailAddress>bill#example.com</EmailAddress>
</Result>
</Results>
I have loaded this XML into an XDocument as such:
string xmlText = GetXML();
XDocument xml = XDocument.Parse(xmlText);
Now, I'm trying to get the results into POCO format. In an effort to do this, I'm currently using:
var objects = from results in xml.Descendants("Results")
select new Results
// I'm stuck
How do I get a collection of Result elements via LINQ? I'm particularly confused about navigating the XML structure at this point in my code.
Thank you!
This will return a IEnumerable of anonymous class:
var q = from result in xml.Descendants
select new
{
ID = result.Descendants("ID"),
Name= result.Descendants("Name"),
EmailAddress= result.Descendants("EmailAddress")
};
or if you have defined class `Result, e.g.:
class Result
{
public ID { get; set; }
public Name { get; set; }
public EmailAddress { get; set; }
}
then:
var q = from result in xml.Descendants
select new Result
{
ID = result.Descendants("ID"),
Name = result.Descendants("Name"),
EmailAddress = result.Descendants("EmailAddress")
};
(returns IEnumerable<Result>)
If your Results child elements are only Result elements, then you can get them like this:
var objects = from result in xml.Descendants
select result;
But in this lucky case you can just use xml.Descendants.
If it's not only Result elements, then this will do fine:
var object = from result in xml.Descendants
where result.Name == "Result"
select result;

Categories

Resources