. 我是 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>
Run Code Online (Sandbox Code Playgroud)
这是我解析这个 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);
}
}
Run Code Online (Sandbox Code Playgroud)
但这给了我运行时异常:
System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name.
Run Code Online (Sandbox Code Playgroud)
所以谁能告诉我我应该如何解决这个异常来获得这些属性的值。. . 提前谢谢。.
前面的部分:称为命名空间前缀。与System.Xml.Linq您合作时,您需要像这样引用它:
child.Attribute("{http://schemas.android.com/apk/res/android}label").Value
Run Code Online (Sandbox Code Playgroud)
您需要使用的 URI 是在 XML 文档中使用xmlns:前缀定义的那个
xmlns:android="http://schemas.android.com/apk/res/android"
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用XNamespace类(请注意,在这种情况下,您没有{}在 URI 周围使用):
XNamespace android = "http://schemas.android.com/apk/res/android";
var attribute = child.Attribute(android + "label").Value;
Run Code Online (Sandbox Code Playgroud)