Reading & Modifying XML Data in C#
-
Hi Guys Hope you can help. I'm writing a program to do a record count within an XML file The XML file is structured as per below:
<WEBRequest>
<Request>
<BlockA>
<REC>
<CustomerNumber></CustomerNumber>
<FirstName></FirstName>
<LastName></LastName>
<Email></Email>
</REC>
</BlockA>
<BlockB>
<REC>
<FirstName></FirstName>
<LastName></LastName>
<OrderDetail></OrderDetail>
<PartType></PartType>
</REC>
<REC>
<FirstName></FirstName>
<LastName></LastName>
<OrderDetail></OrderDetail>
<PartType></PartType>
</REC>
<REC>
<FirstName></FirstName>
<LastName></LastName>
<OrderDetail></OrderDetail>
<PartType></PartType>
</REC>
</BlockB>
<BlockC>
....
....
....
....
</BlockC>
....
....
....
</Request>What I need to do is count and output number of <REC>'s for each <Block> element and output to a cell within excel (Excel Automation is working). E.g. BlockA = 1 REC BlockB = 3 REC . . . BlockM = 32 REC I have the excel automated correctly, however, I'm afraid that my inexperience with XML is quite telling, so I dont have a clear idea on how to do the record count. Thanks in advance!!! Grg!
-
Hi Guys Hope you can help. I'm writing a program to do a record count within an XML file The XML file is structured as per below:
<WEBRequest>
<Request>
<BlockA>
<REC>
<CustomerNumber></CustomerNumber>
<FirstName></FirstName>
<LastName></LastName>
<Email></Email>
</REC>
</BlockA>
<BlockB>
<REC>
<FirstName></FirstName>
<LastName></LastName>
<OrderDetail></OrderDetail>
<PartType></PartType>
</REC>
<REC>
<FirstName></FirstName>
<LastName></LastName>
<OrderDetail></OrderDetail>
<PartType></PartType>
</REC>
<REC>
<FirstName></FirstName>
<LastName></LastName>
<OrderDetail></OrderDetail>
<PartType></PartType>
</REC>
</BlockB>
<BlockC>
....
....
....
....
</BlockC>
....
....
....
</Request>What I need to do is count and output number of <REC>'s for each <Block> element and output to a cell within excel (Excel Automation is working). E.g. BlockA = 1 REC BlockB = 3 REC . . . BlockM = 32 REC I have the excel automated correctly, however, I'm afraid that my inexperience with XML is quite telling, so I dont have a clear idea on how to do the record count. Thanks in advance!!! Grg!
Using XDocument it's quite easy:
XDocument doc = XDocument.Load("YourXMLHere.xml"); var distinctBlocks = from rec in doc.Descendants() where rec.Name.ToString().StartsWith("Block") select rec; var results = from rec in distinctBlocks.Descendants("REC") select rec; var output = (from rec in results group rec by rec.Parent.Name into g select new {Block = g.Key, Count = g.Count()}).ToList(); //dataGridView1.DataSource = output;
modified on Tuesday, April 27, 2010 3:55 PM
-
Using XDocument it's quite easy:
XDocument doc = XDocument.Load("YourXMLHere.xml"); var distinctBlocks = from rec in doc.Descendants() where rec.Name.ToString().StartsWith("Block") select rec; var results = from rec in distinctBlocks.Descendants("REC") select rec; var output = (from rec in results group rec by rec.Parent.Name into g select new {Block = g.Key, Count = g.Count()}).ToList(); //dataGridView1.DataSource = output;
modified on Tuesday, April 27, 2010 3:55 PM
-
here you go. I created a dummy class that holds the Block name and the rec count:
public class BlockCount { private int count = 0; public int Count { get { return count; } set { count = value; } } private string block; public string Block { get { return block; } set { block = value; } } } private void button1\_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load("Abs.xml"); //get the requestNodes XmlNodeList requests = doc.GetElementsByTagName("Request"); if (requests != null && requests.Count > 0) { blocks = new List<BlockCount>(); //now get the blocks nodes foreach (XmlNode nd in requests) { foreach (XmlNode blck in nd.ChildNodes) { //create a new instance of the class BlockCount c = new BlockCount(); //set the name of the block c.Block = blck.Name; foreach (XmlNode rec in blck.ChildNodes) { c.Count++; } blocks.Add(c); } } } //this is just for testing the results witch are fine by the way dataGridView1.DataSource = blocks; } }
modified on Wednesday, April 28, 2010 2:11 PM
-
here you go. I created a dummy class that holds the Block name and the rec count:
public class BlockCount { private int count = 0; public int Count { get { return count; } set { count = value; } } private string block; public string Block { get { return block; } set { block = value; } } } private void button1\_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load("Abs.xml"); //get the requestNodes XmlNodeList requests = doc.GetElementsByTagName("Request"); if (requests != null && requests.Count > 0) { blocks = new List<BlockCount>(); //now get the blocks nodes foreach (XmlNode nd in requests) { foreach (XmlNode blck in nd.ChildNodes) { //create a new instance of the class BlockCount c = new BlockCount(); //set the name of the block c.Block = blck.Name; foreach (XmlNode rec in blck.ChildNodes) { c.Count++; } blocks.Add(c); } } } //this is just for testing the results witch are fine by the way dataGridView1.DataSource = blocks; } }
modified on Wednesday, April 28, 2010 2:11 PM