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. XML / XSL
  4. A very simple problem with XML

A very simple problem with XML

Scheduled Pinned Locked Moved XML / XSL
csharpxmlhelptutorialquestion
5 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.
  • B Offline
    B Offline
    benqazou
    wrote on last edited by
    #1

    Hi, I am new to manipulating XML with C#. I want to know how to retrieve an element value. I have this vey simple XML file : <?xml version="1.0" encoding="utf-8"?> <people> <person> <Lname> Washington </Lname> <FName> George </Fname> </person> <person> <Lname> Lincoln </Lname> <Fname> Abraham </Fname> </person> </people> All I need is a simple method : public string GetFname(string Lname) { ... } which returns the first name by last name. Thanks a lot!

    D S 2 Replies Last reply
    0
    • B benqazou

      Hi, I am new to manipulating XML with C#. I want to know how to retrieve an element value. I have this vey simple XML file : <?xml version="1.0" encoding="utf-8"?> <people> <person> <Lname> Washington </Lname> <FName> George </Fname> </person> <person> <Lname> Lincoln </Lname> <Fname> Abraham </Fname> </person> </people> All I need is a simple method : public string GetFname(string Lname) { ... } which returns the first name by last name. Thanks a lot!

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #2

      hi, There are some different ways how you can do that. Maybe easiest (but not necessary most effective) is this:

      XmlDocument doc = new XmlDocument();
      doc.Load( fileNameofYourXml )
      while( doc.Read() ) // read XML node by node
      {
        if( doc.Name == "Lname" && doc.Value == Lname )
        {
          doc.Read(); // read following node (Fname node)
          return doc.Value; // return its value
        }
      }
      

      OR you can go with XPath:

      doc.SelectSingleNode( string.Format("/person/following-sibling::Lname='{0}'", Lname) );
      

      But I bet I got syntax for that XPath expresion wrong :-O Never forget: "Stay kul and happy" (I.A.)
      David's thoughts / dnhsoftware.org / MyHTMLTidy

      B 1 Reply Last reply
      0
      • D DavidNohejl

        hi, There are some different ways how you can do that. Maybe easiest (but not necessary most effective) is this:

        XmlDocument doc = new XmlDocument();
        doc.Load( fileNameofYourXml )
        while( doc.Read() ) // read XML node by node
        {
          if( doc.Name == "Lname" && doc.Value == Lname )
          {
            doc.Read(); // read following node (Fname node)
            return doc.Value; // return its value
          }
        }
        

        OR you can go with XPath:

        doc.SelectSingleNode( string.Format("/person/following-sibling::Lname='{0}'", Lname) );
        

        But I bet I got syntax for that XPath expresion wrong :-O Never forget: "Stay kul and happy" (I.A.)
        David's thoughts / dnhsoftware.org / MyHTMLTidy

        B Offline
        B Offline
        benqazou
        wrote on last edited by
        #3

        Hi, thanks for your reply, In fact I tried the first method. It doesn't work. 1-XmlDocument has no method Read, (only a method ReadNode which take a reader as argument! No idea how it works) 2-Instead I used XmlTextReader, still not working. One remark: when I display all the Values and Names in a label, I get many of them null. Other think, they are always the same : value=name? Any hint? For the Xpath method didn't try it. Thanks.

        D 1 Reply Last reply
        0
        • B benqazou

          Hi, thanks for your reply, In fact I tried the first method. It doesn't work. 1-XmlDocument has no method Read, (only a method ReadNode which take a reader as argument! No idea how it works) 2-Instead I used XmlTextReader, still not working. One remark: when I display all the Values and Names in a label, I get many of them null. Other think, they are always the same : value=name? Any hint? For the Xpath method didn't try it. Thanks.

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          benqazou wrote: 1-XmlDocument has no method Read, (only a method ReadNode which take a reader as argument! No idea how it works) Oh yes, sorry. My bad. :-O Of course Read() is method of XmlReader. benqazou wrote: 2-Instead I used XmlTextReader, still not working. One remark: when I display all the Values and Names in a label, I get many of them null. Other think, they are always the same : value=name? Can you post snippet of your code? Never forget: "Stay kul and happy" (I.A.)
          David's thoughts / dnhsoftware.org / MyHTMLTidy

          1 Reply Last reply
          0
          • B benqazou

            Hi, I am new to manipulating XML with C#. I want to know how to retrieve an element value. I have this vey simple XML file : <?xml version="1.0" encoding="utf-8"?> <people> <person> <Lname> Washington </Lname> <FName> George </Fname> </person> <person> <Lname> Lincoln </Lname> <Fname> Abraham </Fname> </person> </people> All I need is a simple method : public string GetFname(string Lname) { ... } which returns the first name by last name. Thanks a lot!

            S Offline
            S Offline
            softty
            wrote on last edited by
            #5

            Hi, since it is a simple requirement, I have placed a simple solution Try this.. ////////////////////////////////////////// using System; using System.Xml; namespace ConsoleApplication1 { /* c:\pk.xml Washington George Lincoln Abraham */ class XmlTest { XmlDocument doc;// public XmlTest() { doc = new XmlDocument(); doc.Load("c:\\pk.xml"); // put your file path } public void Run() { while(true) { Console.Write("\nEnter a Name: "); string input = Console.ReadLine(); if(input == "" || input==null) break; Console.WriteLine("Your FName is " + GetFName(input)); } } public string GetFName(string lname) { string ret = "Unknown" ; XmlNodeList lnode = doc.GetElementsByTagName("Lname"); for(int i=0; i< lnode.Count; i++) { if(lnode.Item(i).InnerText.Trim() == lname.Trim()) { ret = lnode.Item(i).ParentNode.LastChild.InnerText; break; } } return ret; } public static void Main() { XmlTest xmlDoc = new XmlTest(); xmlDoc.Run(); } } } love2code

            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