如何访问包含“:”字符的xml属性?

3
我是c#的新手。我正在尝试解析一个“AndroidManifest.xml”文件。在解析时,我需要获取某些元素的一些属性值。以下是我的“AndroidManifest.xml”文件:

<?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>

这是我用来解析此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);
            }
    }

但这会导致我运行时异常,如下所示:
System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name.

所以,有人能告诉我应该如何解决这个异常来获取这些属性的值吗... 提前感谢。

可能是XSLT:源XML中属性名称中的冒号的重复问题。 - alkar
2个回答

5
:前面的部分被称为命名空间前缀。在使用System.Xml.Linq时,您需要像这样引用它:
child.Attribute("{http://schemas.android.com/apk/res/android}label").Value

您需要使用的URI是在XML文档中使用xmlns:前缀定义的那个。
xmlns:android="http://schemas.android.com/apk/res/android"

另一种方法是使用XNamespace类(请注意,在这种情况下,您不会在URI周围使用{}):

XNamespace android = "http://schemas.android.com/apk/res/android";
var attribute = child.Attribute(android + "label").Value;

1

另一种选择是使用 LocalName 属性。

获取 APK 的 versionName 的示例:

XDocument doc = XDocument.Load(xmlfile);
  var nList = doc.Root.Attributes();

  foreach (var att in nList)
  {
      if (att.Name.LocalName == "versionName")
         return att.Value;
  }       

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接