Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading & Modifying XML Data in C#

Reading & Modifying XML Data in C#

Scheduled Pinned Locked Moved C#
csharptestingtoolsxmlhelp
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrgBalden
    wrote on last edited by
    #1

    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!

    D 1 Reply Last reply
    0
    • G GrgBalden

      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!

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • D Dan Mos

        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

        G Offline
        G Offline
        GrgBalden
        wrote on last edited by
        #3

        Thanks for Info! I don't suppose you have the c# code?

        D G 2 Replies Last reply
        0
        • G GrgBalden

          Thanks for Info! I don't suppose you have the c# code?

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          GrgBalden wrote:

          I don't suppose you have the c# code?

          That was C#(3) code. Using XLinq => Linq to XML.

          modified on Wednesday, April 28, 2010 10:33 AM

          1 Reply Last reply
          0
          • G GrgBalden

            Thanks for Info! I don't suppose you have the c# code?

            G Offline
            G Offline
            GrgBalden
            wrote on last edited by
            #5

            Hi can anyone else help with this, I'm somewhat limited to c#2 (using VS2005)??

            D 1 Reply Last reply
            0
            • G GrgBalden

              Hi can anyone else help with this, I'm somewhat limited to c#2 (using VS2005)??

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #6

              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

              G 1 Reply Last reply
              0
              • D Dan Mos

                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

                G Offline
                G Offline
                GrgBalden
                wrote on last edited by
                #7

                Thank you kindly!!! Will give this a go tommorow!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups