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. hi, how can i read xmlnode in c#.net [modified]

hi, how can i read xmlnode in c#.net [modified]

Scheduled Pinned Locked Moved C#
csharpxmlquestion
7 Posts 3 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.
  • P Offline
    P Offline
    premkamalg
    wrote on last edited by
    #1

    hi, i want to read one particular Xml_node in c#.net how can i read that node plz give sample code for that . in the below xml file i want read node only and that phone_no child nodes text is stored in listbox. /* < < <a <b <c < < <9898776 <8272679 <7t8124748 < < <gsdghgkef <guiqweu3r <io34uiiju < < */ Thanks -- modified at 2:07 Wednesday 13th September, 2006

    A C 2 Replies Last reply
    0
    • P premkamalg

      hi, i want to read one particular Xml_node in c#.net how can i read that node plz give sample code for that . in the below xml file i want read node only and that phone_no child nodes text is stored in listbox. /* < < <a <b <c < < <9898776 <8272679 <7t8124748 < < <gsdghgkef <guiqweu3r <io34uiiju < < */ Thanks -- modified at 2:07 Wednesday 13th September, 2006

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Hi, To read the XML file or string you can use the XmlDocument class which is in the System.XML namespace.

      XmlDocument doc = new XmlDocument();
      doc.Load("xmlstring");
      doc.LoadXML("xmlfile");
      XmlNode root = doc.DocumentElement;
      XmlNodeList list = root.ChildNodes;
      foreach(XmlNode node in list)
      {
      if(node.HasChildNodes)
      {
      XmlNodeList list1 = node.ChildNodes;
      foreach(....)
      {

      }
      }
      else
      {
      string name=node.Name;
      string text=node.InnerText;
      }
      }

      Hope this code helps you.

      Do your best to be the best

      1 Reply Last reply
      0
      • P premkamalg

        hi, i want to read one particular Xml_node in c#.net how can i read that node plz give sample code for that . in the below xml file i want read node only and that phone_no child nodes text is stored in listbox. /* < < <a <b <c < < <9898776 <8272679 <7t8124748 < < <gsdghgkef <guiqweu3r <io34uiiju < < */ Thanks -- modified at 2:07 Wednesday 13th September, 2006

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        You would use XmlDocument, but you'd use XPath to go direct to your node, as in foreach(XmlNode node in doc.SelectNodes("//deatails/phone_no/No")) And to do this, you'd fix your schema and replace No1, No2 and No3 with nodes that all have the same name ( and a number as an attribute if you need it for number, but the order will never change anyhow ).

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        P 1 Reply Last reply
        0
        • C Christian Graus

          You would use XmlDocument, but you'd use XPath to go direct to your node, as in foreach(XmlNode node in doc.SelectNodes("//deatails/phone_no/No")) And to do this, you'd fix your schema and replace No1, No2 and No3 with nodes that all have the same name ( and a number as an attribute if you need it for number, but the order will never change anyhow ).

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          P Offline
          P Offline
          premkamalg
          wrote on last edited by
          #4

          hi , thanks for giving helpful idea. i try that but in, i can't get the data of node plz check my code give any modifications. iam new to c#.net and xml private void button1_Click(object sender, System.EventArgs e) { string filename = "C:\\C#program\\cs_programs\\singlenode\\bin\\Debug\\nodexml.xml"; XmlTextReader tr = new XmlTextReader(filename); XmlDocument doc = new XmlDocument(); while(tr.Read()) { foreach(XmlNode node in doc.SelectNodes("//details/add/addr")) listBox1.Items.Add(doc.Value); } }

          C 1 Reply Last reply
          0
          • P premkamalg

            hi , thanks for giving helpful idea. i try that but in, i can't get the data of node plz check my code give any modifications. iam new to c#.net and xml private void button1_Click(object sender, System.EventArgs e) { string filename = "C:\\C#program\\cs_programs\\singlenode\\bin\\Debug\\nodexml.xml"; XmlTextReader tr = new XmlTextReader(filename); XmlDocument doc = new XmlDocument(); while(tr.Read()) { foreach(XmlNode node in doc.SelectNodes("//details/add/addr")) listBox1.Items.Add(doc.Value); } }

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Like I said, your schema is useless. You need to change the addr1/addr2/addr3 nodes to all be just addr. And don't forget, XML is case sensitive.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            P 1 Reply Last reply
            0
            • C Christian Graus

              Like I said, your schema is useless. You need to change the addr1/addr2/addr3 nodes to all be just addr. And don't forget, XML is case sensitive.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              P Offline
              P Offline
              premkamalg
              wrote on last edited by
              #6

              hi Christian i new to c#.net i want to access one node in xmlfile using c#.net . that node have child node (i want that child node values) i write my xml file like this < < <Ap <MP <UP < <gjsefgjkl<dfhtyxcvd<ioiopweri < in this i want to access the "add" child node value. i wrote c#.net code like this, "When i press Button the childnode("add") values r stored in listbox " plz change any modification in my code or plz give code how to access thatnode private void button1_Click(object sender, System.EventArgs e) { string filename = "C:\\C#program\\cs_programs\\singlenode\\bin\\Debug\\nodexml.xml"; XmlTextReader tr = new XmlTextReader(filename); XmlDocument doc = new XmlDocument(); while(tr.Read()) { foreach(XmlNode node in doc.SelectNodes("//details/add/addr")) listBox1.Items.Add(tr.Value); } } Thanks

              C 1 Reply Last reply
              0
              • P premkamalg

                hi Christian i new to c#.net i want to access one node in xmlfile using c#.net . that node have child node (i want that child node values) i write my xml file like this < < <Ap <MP <UP < <gjsefgjkl<dfhtyxcvd<ioiopweri < in this i want to access the "add" child node value. i wrote c#.net code like this, "When i press Button the childnode("add") values r stored in listbox " plz change any modification in my code or plz give code how to access thatnode private void button1_Click(object sender, System.EventArgs e) { string filename = "C:\\C#program\\cs_programs\\singlenode\\bin\\Debug\\nodexml.xml"; XmlTextReader tr = new XmlTextReader(filename); XmlDocument doc = new XmlDocument(); while(tr.Read()) { foreach(XmlNode node in doc.SelectNodes("//details/add/addr")) listBox1.Items.Add(tr.Value); } } Thanks

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Well, if you want to access the add node, just drop the rest of the XPath - /details/add If you want to get all the addr nodes, name them all addr, as I advised you to do in the first place.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                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