How to access xml attributes which contains ':' charactor in their name?
c#, xml
Solution
The part before `:` is called the namespace prefix. When working with `System.Xml.Linq` you need to reference it like this:
child.Attribute("{http://schemas.android.com/apk/res/android}label").Value
The URI you need to use is the one that is defined in the XML document using `xmlns:` prefix as
xmlns:android="http://schemas.android.com/apk/res/android"
An alternative way is to use the `XNamespace` class (note that in this case you are not using `{}` around the URI):
XNamespace android = "http://schemas.android.com/apk/res/android";
var attribute = child.Attribute(android + "label").Value;
Problem
. I am new to c# . I am trying to parse an "AndroidManifest.xml" file. While parsing i need to get the values of the some attributes of some elements. Following is my "AndroidManifest.xml" file: ``` <?xml version="1.0" encoding="utf-8"?> <manifest android:versionCode="2" android:versionName="1.1.0" package="com.sbi.SBIFreedomPlus" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:label="@string/app_name" android:icon="@drawable/icon"> <activity android:label="@string/app_name" android:name=".SBIFreedom" android:launchMode="singleTask" android:screenOrientation="sensor" android:configChanges="locale|keyboardHidden|orientation|screenSize" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.konylabs.android.KonyMapsActivity" android:launchMode="singleInstance" /> <activity android:name="com.konylabs.android.KonyMapsV2Activity" /> </application> <supports-screens android:anyDensity="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </manifest> ``` Here is my code to parse this xml : ``` void read() { try { string fileName = @"tempassets//AndroidManifest.xml"; String applicationPackage = ""; XDocument doc = XDocument.Load(fileName); applicationPackage = doc.Root.Attribute("package").Value; foreach (XElement el in doc.Root.Elements()) { if (el.Name == "application") { foreach (XElement child in el.Elements()) { if (child.HasElements) { MessageBox.Show("true"); MessageBox.Show("name:" + child.Name + "Label" + child.Attribute("android:label").Value); } } } } } catch (Exception objException) { MessageBox.Show("exception while parsing AndroidManifest.xml :"+objException); File.WriteAllText(@"tempassets//Config//error.txt", ""+objException, Encoding.Unicode); } } ``` But this gives me runtime exception as: ``` System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name. ``` So can anyone tell me how should i resolve this exception to get values of such attribtes . . . Thanx in advance . .