Created
March 25, 2015 18:22
-
-
Save Microsofttechies/5f1414dff2d31bb643de to your computer and use it in GitHub Desktop.
C# Read xml Attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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