Posts linq to xml file
Post
Cancel

linq to xml file

1
2
3
4
5
6
7
8
//test.xml
<xml>
  <root>
	<row product_line="test1" product_name="DBManager" ref_no="u3y5v843"/>
	<row product_line="test2" product_name="ASDSDAS" ref_no="23423423"/>
	<row product_line="test3" product_name="5SDSFS" ref_no="67873"/>
  </root>
</xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//read xml
XDocument xdoc = XDocument.Load(file2scan);

//query1
var DBManager_recs = from p in xdoc.Root.Element("root").Elements("row")
			where ((string)p.Attribute("product_name") == "DBManager")
			select p;

//create a datatable
DataTable cloneTableC = new DataTable();
cloneTableC.Columns.Add("Line");
cloneTableC.Columns.Add("product");
cloneTableC.Columns.Add("ReferenceNo");

//add it to datatable
foreach (var item in DBManager_recs.ToList())
{
	cloneTableC.Rows.Add(new string[] { (string)item.Attribute("product_line"), (string)item.Attribute("product_name"), (string)item.Attribute("ref_no") });
}

//give dt to datagridview
dg_argos_country.DataSource = cloneTableC;

//query2
var recs2 = from p in xdoc.Root.Element("root").Elements("row")
				 where ((string)p.Attribute("product_name") == "DBManager" && (string)p.Attribute("product_line") != "Translator")
				 select p;

if you like you can generate XSD from XML via http://xmlgrid.net/xml2xsd.html

origin - http://www.pipiscrew.com/?p=6052 linq-to-xml-file

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags