How to Search in XML and show results in GridView in C#? - 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. :)

Related

Linq to XML query analyz

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")
});

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);
}

Parsing (and keeping) XML structure into SQL Server

I'm looking to parse a relatively complex XML file through C# and store a selection of the data into a SQL Server '08 database. This is what I'm looking to extract from the XML file:
<educationSystem>
<school>
<name>Primary School</name>
<students>
<student id="123456789">
<name>Steve Jobs</name>
<other elements>More Data</other elements>
</student>
<student id="987654">
<name>Jony Ive</name>
<otherElements>More Data</otherElements>
</student>
</students>
</school>
<school>
<name>High School</name>
<students>
<student id="123456">
<name>Bill Gates</name>
<other elements>More Data</other elements>
</student>
<student id="987654">
<name>Steve Ballmer</name>
<otherElements>More Data</otherElements>
</student>
</students>
</school>
</educationSystem>
[Before you ask, no this isn't a school assignment - I'm using school/students as an example and because the original is a lot more sensitive.]
I'm able to (using XDocument/XElement) parse the XML file and get a list of all school names, student names and student ID's, but when this gets added to the database, I end up with the Bill Gates student entry being under a second school. It's all just line-by-line.
I'm looking to find a way to say, achieve this:
Foreach school
put it's name into an XElement
foreach student
grab the name and id put into XElements
Grab next school and repeat
I believe Linq would be the best way to achieve this, but I'm having trouble in how to get started with the process. Would anyone be able to point me in the right direction?
Edit: Here's the code I'm currently using to save data to the database. It processes a list at a time (hence things aren't related as they should be). I'll also be tidying up the SQL as well.
private void saveToDatabase (List<XElement> currentSet, String dataName)
{
SqlConnection connection = null;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString + "; Asynchronous Processing=true";
connection = new SqlConnection(connectionString);
connection.Open();
foreach (XElement node in currentSet)
{
SqlCommand sqlCmd = new SqlCommand("INSERT INTO dbo.DatabaseName (" + dataName + ") VALUES ('" + node.Value + "')", connection);
sqlCmd.ExecuteNonQuery();
}
}
This LINQ will generate a Collection of Objects,with two properties
Name of the school
List of students(again a collection)
var result = XElement.Load("data.xml")
.Descendants("school")
.Select( x => new {
name = XElement.Parse(x.FirstNode.ToString()).Value,
students =x.Descendants("student")
.Select(stud => new {
id = stud.Attribute("id"),
name = XElement.Parse(stud.FirstNode.ToString()).Value})
.ToList()});
Note:The LINQ assumes <name> as the first node under <school> and <student> tags
Then you can use the foreach that you intended and it will work like a charm
foreach (var school in result)
{
var schoolName = school.name;
foreach (var student in school.students)
{
//Access student.id and student.name here
}
}
For this particular type of workings with XML data, you could use XML Serialization / Deserialization.
This will allow you to Deserialize your XML Data into a IEnumerable Class Object, Perform your LINQ Queries on this Class and then save to SQL.
Hope this helps.
Update: The original code example did not mention a namespace. Namespaces need to be either accounted for when searching for elements by XName or one needs to to search using the XName.LocalName property. Updated the example to show how to handle selecting elements in such a case.
namespace Stackover
{
using System;
using System.Xml.Linq;
class Program
{
private const string Xml = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<namespaceDocument xmlns=""http://www.namedspace/schemas"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.namedspace/schemas.xsd"">
<educationSystem>
<school>
<name>Primary School</name>
<students>
<student id=""123456789"">
<name>Steve Jobs</name>
<otherElements>
<dataA>data</dataA>
</otherElements>
</student>
<student id=""987654"">
<name>Jony Ive</name>
<otherElements>
<dataB>data</dataB>
</otherElements>
</student>
</students>
</school>
<school>
<name>High School</name>
<students>
<student id=""123456"">
<name>Bill Gates</name>
<otherElements>
<dataC>data</dataC>
</otherElements>
</student>
<student id=""987654"">
<name>Steve Ballmer</name>
<otherElements>
<dataD>data</dataD>
</otherElements>
</student>
</students>
</school>
</educationSystem>
</namespaceDocument>";
static void Main(string[] args)
{
var root = XElement.Parse(Xml);
XNamespace ns = "http://www.namedspace/schemas";
foreach(var school in root.Descendants(ns + "school")) // or root.Descendants().Where(e => e.Name.LocalName.Equals("school"));
{
Console.WriteLine(school.Element(ns + "name").Value);
foreach (var students in school.Elements(ns+ "students"))
{
foreach (var student in students.Elements())
{
Console.WriteLine(student.Attribute("id"));
Console.WriteLine(student.Name); // Name = namespace + XName
Console.WriteLine(student.Name.LocalName); // no namespace
}
}
}
}
}
}

XML Serialization of member variable as xmlnode

I have some XML which I would like to serialize into a class.
<Group>
<Employees>
<Employee>
<Name>Hari</Name>
<Age>30</Age>
</Employee>
<Employee>
<Name>Yougov</Name>
<Age>31</Age>
</Employee>
<Employee>
<Name>Adrian</Name>
<Age>28</Age>
</Employee>
</Employees >
The above XML can be realized in C# pretty much easily.
But I'm stumbled upon my requirement, where the XML looks like,
<Group>
<Employees>
<Hari Age=30 />
<Yougov Age=31 />
<Adrian Age=28 />
</Employees >
</Group>
Where Employees is a List<Employee> with KeyValuePair<string, int>("Hari", 30)
How do I design the classes and member variables to get the above serialized XML?
(Provided, there wont be any duplicate names in the employee list)
Any help is much appreciated.
*Serializing KeyValuePair
I would go with Linq2Xml in your case.
XDocument xDoc = XDocument.Load(......);
var emps = xDoc.Descendants("Employees").Elements()
.Select(x => new Employee() {
Name = x.Name.ToString(),
Age = int.Parse(x.Attribute("Age").Value)
})
.ToList();
PS: Age=30 is not valid. It shoudl be Age="30"
It is not a good idea to use the data as the schema; in particular, an awful lot of names are not valid as xml element names, but also: it just isn't good practice (for example, it makes it virtually useless in terms of schema validation). If you want to be terse, maybe something like:
<Employees>
<Add Name="Hari" Age="30" />
</Employees>
or
<Employees>
<Employee Name="Hari" Age="30" />
</Employees>
which can be done simply with:
[XmlArray("Employees"), XmlArrayItem("Employee")]
public List<Employee> Employees {get;set;}
and:
public class Employee {
[XmlAttribute]
public string Name {get;set;}
[XmlAttribute]
public int Age {get;set;}
}
XmlSerializer does not support the "content as an element name" serializer, unless you do everything yourself with IXmlSerializable from the parent element (it would have to be from the parent, as XmlSerializer would have no way of identifying the child to handle there).

XML with C# LINQ to XML

Following is the example of an XML document.
<People>
<Person>
<Name>ABC </Name>
<SSN>111111</SSN>
<Address>asdfg</Address>
</Person>
</People>
I need to get the tag names but not the values between the tag names. That is, under the person tag, I should grab the Name, SSN, and Address nodes and not the ABC, 111111, and asdfg values.
I need to use LINQ to XML and query it in C#.
How can I do it?
This returns the names as a list of strings:
var doc = XDocument.Parse(#"<People>
<Person>
<Name>ABC </Name>
<SSN>111111</SSN>
<Address>asdfg</Address>
</Person>
</People>"
);
var list = doc.Root.Element("Person").Descendants()
.Select(node => node.Name.LocalName).ToList();
In case you're using an XElement instead of an XDocument, you can remove the .Root from the above code and get the correct results.
Create a class
public class Person
{
public string Name {get; set;}
public int SSN {get; set;}
public string Address {get; set;}
}
And create a new person this way;
List<Person> NewPersons = new List<Person>();
XDocument doc = XDocument.Load(xmlFile);
foreach(XElement xElem in doc.Descendants("Person"))
{
NewPersons.Add(new Person
{
Name = xElem. Element("Name").Value,
SSN = xElem.Element("SSN").Value,
Address = xElem.Element("Address").Value,
});
}
You can get it this way...
string xml = "<People> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> </People>";
XElement xmlData = XElement.Parse(xml);
foreach(XElement xmlPerson in xmlData.Elements("Person"))
{
List<string> TagsForThisPerson = new List<string>();
foreach(XElement xmlTag in xmlPerson.Elements())
{
TagsForThisPerson.Add(xmlTag.Name.ToString());
}
TagsForThisPerson.Dump();
}
Simple LINQ syntax to get the names is listed below. It assumes you have the XML loaded in a XDocument variable named doc.
var nodeNames = from node in doc.Descendants("Person").First().Descendants()
select node.Name.LocalName;
It only looks at the first person. If you have more than one in the XML document, the list of names is probably not what you would want (no reason to repeat all the names of the nodes over and over). This way, you get a list of just the node names for the first person, but it does assume that the first one would have a complete list of names. If they vary, you would need to build a distinct list from all the nodes.

Categories

Resources