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