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. how to read value at specifix XML node

how to read value at specifix XML node

Scheduled Pinned Locked Moved C#
tutorialquestionxml
6 Posts 4 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
    bimbambumbum
    wrote on last edited by
    #1

    Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node

    and in case it requires a different search method I would also like to know how to obtain the value at node

    0

    ? Anxious to learn how to do this Best Regards Tom

      0
      0
    
    0
      0
    
    0
      0
    

    0
    0
    0
    0
    0

    J P H 3 Replies Last reply
    0
    • B bimbambumbum

      Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node

      and in case it requires a different search method I would also like to know how to obtain the value at node

      0

      ? Anxious to learn how to do this Best Regards Tom

        0
        0
      
      0
        0
      
      0
        0
      

      0
      0
      0
      0
      0

      J Offline
      J Offline
      Jason Gleim
      wrote on last edited by
      #2

      LINQ is your friend when walking XML files. There are a lot of examples using Linq queries over XML to get data out of the file. I'm too lazy right now to Google it or search here on CP so I'll leave that to you. But that should get you what you want.

      1 Reply Last reply
      0
      • B bimbambumbum

        Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node

        and in case it requires a different search method I would also like to know how to obtain the value at node

        0

        ? Anxious to learn how to do this Best Regards Tom

          0
          0
        
        0
          0
        
        0
          0
        

        0
        0
        0
        0
        0

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        How about this: //xx[@atty='3' and @attz='0']/*[name()='min' or name()='max']

        B 1 Reply Last reply
        0
        • B bimbambumbum

          Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node

          and in case it requires a different search method I would also like to know how to obtain the value at node

          0

          ? Anxious to learn how to do this Best Regards Tom

            0
            0
          
          0
            0
          
          0
            0
          

          0
          0
          0
          0
          0

          H Offline
          H Offline
          HobbyProggy
          wrote on last edited by
          #4

          Hey bimbambumbum, that's my way of doing it, with this code you'll get the attributes to your specific node. Code is written for .Net Framework 3.5 Additionaly i posted the XML file so you can backtrace the work and derive your code from it. All you need to have set up is the xmlDoc and a xPathNavigator xmlNavi = xmlDoc.createNavi...

          public DataTable LoadValueConfig(string ControlName, string ParentName, string FormName)
          {
          DataTable _dt = new DataTable();
          _dt.Columns.Add("Name");
          _dt.Columns.Add("Value");

                  //Move to root node
                  \_xmlNavi.MoveToRoot();
                  //move to windowConfig Node
                  \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode("WindowConfig"));
                  //if node of form exists move to it
                  if (FormName != "") { \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(FormName)); }
                  //if node of parent exists move to it e.g. bsAdminTest
                  \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ParentName));
                  // if control Name exists move to it e.g. switchButton1
                  if (\_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName)))
                  {
                      \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName));
                      //Move to first attribute
                      \_xmlNavi.MoveToFirstAttribute();
                      // move through all Attributes and save the values into the DataTable
                      do
                      {
                          if (\_xmlNavi.Value != "")
                          {
                              \_dt.Rows.Add(\_xmlNavi.Name, \_xmlNavi.Value);
                          }
                      // while able to move to next Attribute
                      } while(\_xmlNavi.MoveToNextAttribute());
                  }
                  
                  return \_dt;
              }
          

          XML File:

          <WindowConfig>
          <kopiefrmHauptfenster Position="460:21" Size="878:1170" WindowState="Normal">
          <checkBoxItem1 chBoxCheck="True" />
          <bsAdminTest Active="False">
          <switchButton1 switch="False" />
          <richTextBoxEx1 Text="" />
          </bsAdminTest>
          <Project Active="True">
          <navigationPane1 CheckedButton="Projekte" Expanded="True" />
          <expandablePanel1 Expanded="True" />
          </Project>
          </kopiefrmHauptfenster>
          </WindowConfig>

          Edit:

          B 1 Reply Last reply
          0
          • H HobbyProggy

            Hey bimbambumbum, that's my way of doing it, with this code you'll get the attributes to your specific node. Code is written for .Net Framework 3.5 Additionaly i posted the XML file so you can backtrace the work and derive your code from it. All you need to have set up is the xmlDoc and a xPathNavigator xmlNavi = xmlDoc.createNavi...

            public DataTable LoadValueConfig(string ControlName, string ParentName, string FormName)
            {
            DataTable _dt = new DataTable();
            _dt.Columns.Add("Name");
            _dt.Columns.Add("Value");

                    //Move to root node
                    \_xmlNavi.MoveToRoot();
                    //move to windowConfig Node
                    \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode("WindowConfig"));
                    //if node of form exists move to it
                    if (FormName != "") { \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(FormName)); }
                    //if node of parent exists move to it e.g. bsAdminTest
                    \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ParentName));
                    // if control Name exists move to it e.g. switchButton1
                    if (\_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName)))
                    {
                        \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName));
                        //Move to first attribute
                        \_xmlNavi.MoveToFirstAttribute();
                        // move through all Attributes and save the values into the DataTable
                        do
                        {
                            if (\_xmlNavi.Value != "")
                            {
                                \_dt.Rows.Add(\_xmlNavi.Name, \_xmlNavi.Value);
                            }
                        // while able to move to next Attribute
                        } while(\_xmlNavi.MoveToNextAttribute());
                    }
                    
                    return \_dt;
                }
            

            XML File:

            <WindowConfig>
            <kopiefrmHauptfenster Position="460:21" Size="878:1170" WindowState="Normal">
            <checkBoxItem1 chBoxCheck="True" />
            <bsAdminTest Active="False">
            <switchButton1 switch="False" />
            <richTextBoxEx1 Text="" />
            </bsAdminTest>
            <Project Active="True">
            <navigationPane1 CheckedButton="Projekte" Expanded="True" />
            <expandablePanel1 Expanded="True" />
            </Project>
            </kopiefrmHauptfenster>
            </WindowConfig>

            Edit:

            B Offline
            B Offline
            bimbambumbum
            wrote on last edited by
            #5

            Thanks gentlemen..this looks like a great starting point! Appreciate it

            1 Reply Last reply
            0
            • P PIEBALDconsult

              How about this: //xx[@atty='3' and @attz='0']/*[name()='min' or name()='max']

              B Offline
              B Offline
              bimbambumbum
              wrote on last edited by
              #6

              Hi Can I please ask you to elaborate on your approach? For example provide an example. I tried this:

                      XmlDocument d = new XmlDocument();
                      d.Load("XmlData2.xml");
                      var tmp = d.SelectSingleNode("//xx\[@atty='3' and @attz='0'\]/\*\[name()='min' or name()='max'\]");
                      //var tmp = d.SelectSingleNode("//xx\[@atty='3' and @attz='0'\]/\*\[name()='min'\]");
              

              but I don't get the right values when inspecting the tmp variable. Thanks

              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