Skip to content

Instantly share code, notes, and snippets.

@Microsofttechies
Created March 25, 2015 18:22
Show Gist options
  • Select an option

  • Save Microsofttechies/5f1414dff2d31bb643de to your computer and use it in GitHub Desktop.

Select an option

Save Microsofttechies/5f1414dff2d31bb643de to your computer and use it in GitHub Desktop.
C# Read xml Attributes
XMl file
<?xml version="1.0" encoding="utf-8" ?>
<Addresses>
<Address key="UK" City="London" Zip="9999"></Address>
<Address Key="US" City="Newyork" Zip="0000"></Address>
</Addresses>
C#
string strF = "AddressesData.xml";
if (File.Exists(strF))
{
XElement root = XElement.Load(strF);
IEnumerable<XElement> address =
from el in root.Elements("Address")
where (string)el.Attribute("key") == "UK"
select el;
foreach (XElement el in address)
{
string city = el.Attribute("City").Value;
string zip = el.Attribute("Zip").Value;
}
}
else
{ Console.WriteLine("No file"); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment