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. C# XML Query Website address based on Website name

C# XML Query Website address based on Website name

Scheduled Pinned Locked Moved XML / XSL
csharpdatabasecomxmlquestion
6 Posts 2 Posters 4 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.
  • W Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    Good afternoon. I have the following XML file structure:

    <?xml version="1.0" ?>

    • <WEB_SITE>
    • <RECORD>
      <Name>Homepage</Name>
      <Address>http://www.google.com</Address>
      </RECORD>
      </WEB_SITE>

    I would like to derive the URL from the XML file if the name is Homepage. I have this so far:

    string strtmpName = string.Empty;
    string strtmpAddress = string.Empty;

            XmlTextReader reader = null;
            XDocument xmlDoc = new XDocument();
            reader = new XmlTextReader(cv.StrIndivPath1 + strLANID + cv.StrIndivPath2 + "Website.xml");
            xmlDoc = XDocument.Load(reader);
            reader.Close();
            XElement Results = XElement.Parse(xmlDoc.ToString());
    
            foreach (XElement xe in Results.Elements("RECORD").Descendants())
            {
                switch (xe.Name.ToString())
                {
                    case "Name":
                        if (xe.Value.ToLower() == "homepage")
                        {
                            strtmpAddress = Results.Elements("RECORD").Descendants("Address").ToString();
                            MessageBox.Show(strtmpAddress);
                        }                        
                        break;
                    //case "Address":
                    //    strtmpAddress = xe.Value;
                    //    if (txtAddress.Text == strtmpAddress)
                    //    {
                    //        AddressFlag = true;
                    //    }
                    //    break;
                    default:
                        break;
                }
            }
    

    Any suggestions? Thank you, WHEELS

    T 1 Reply Last reply
    0
    • W Wheels012

      Good afternoon. I have the following XML file structure:

      <?xml version="1.0" ?>

      • <WEB_SITE>
      • <RECORD>
        <Name>Homepage</Name>
        <Address>http://www.google.com</Address>
        </RECORD>
        </WEB_SITE>

      I would like to derive the URL from the XML file if the name is Homepage. I have this so far:

      string strtmpName = string.Empty;
      string strtmpAddress = string.Empty;

              XmlTextReader reader = null;
              XDocument xmlDoc = new XDocument();
              reader = new XmlTextReader(cv.StrIndivPath1 + strLANID + cv.StrIndivPath2 + "Website.xml");
              xmlDoc = XDocument.Load(reader);
              reader.Close();
              XElement Results = XElement.Parse(xmlDoc.ToString());
      
              foreach (XElement xe in Results.Elements("RECORD").Descendants())
              {
                  switch (xe.Name.ToString())
                  {
                      case "Name":
                          if (xe.Value.ToLower() == "homepage")
                          {
                              strtmpAddress = Results.Elements("RECORD").Descendants("Address").ToString();
                              MessageBox.Show(strtmpAddress);
                          }                        
                          break;
                      //case "Address":
                      //    strtmpAddress = xe.Value;
                      //    if (txtAddress.Text == strtmpAddress)
                      //    {
                      //        AddressFlag = true;
                      //    }
                      //    break;
                      default:
                          break;
                  }
              }
      

      Any suggestions? Thank you, WHEELS

      T Offline
      T Offline
      Tarun K S
      wrote on last edited by
      #2

      XPath can be used. Here is how it works:

      String myXPath="/WEB_SITE/RECORD[Name='Homepage']/Address";
      XmlNodeList myNodeList=xmlDoc.SelectNodes(myXPath); // This gives you the collection of Address nodes with Name as Homepage
      XmlNode myXmlNode;

      foreach (myXmlNode in xmlNodeList)
      {
      MessageBox.Show(myXmlNode.Value); // Displays the address
      }

      Cheers!

      W 1 Reply Last reply
      0
      • T Tarun K S

        XPath can be used. Here is how it works:

        String myXPath="/WEB_SITE/RECORD[Name='Homepage']/Address";
        XmlNodeList myNodeList=xmlDoc.SelectNodes(myXPath); // This gives you the collection of Address nodes with Name as Homepage
        XmlNode myXmlNode;

        foreach (myXmlNode in xmlNodeList)
        {
        MessageBox.Show(myXmlNode.Value); // Displays the address
        }

        Cheers!

        W Offline
        W Offline
        Wheels012
        wrote on last edited by
        #3

        I am getting a Type and identifier are both required in a forach statement.

        T 1 Reply Last reply
        0
        • W Wheels012

          I am getting a Type and identifier are both required in a forach statement.

          T Offline
          T Offline
          Tarun K S
          wrote on last edited by
          #4

          well you have to add this : using System.Xml.Xpath; so that it can identify the XPath.

          W 1 Reply Last reply
          0
          • T Tarun K S

            well you have to add this : using System.Xml.Xpath; so that it can identify the XPath.

            W Offline
            W Offline
            Wheels012
            wrote on last edited by
            #5

            Excellent. That does the trick. Thank you.

            T 1 Reply Last reply
            0
            • W Wheels012

              Excellent. That does the trick. Thank you.

              T Offline
              T Offline
              Tarun K S
              wrote on last edited by
              #6

              Cheers!

              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