我有一個 txt 檔案,其中包含
<?xml version="1.0" blablabla ..etc>
<uses-permission ~~~~~~ ..etc>
<application ~~~some of words i dont know it and changes from file to another file~~~ >
</application>
</manifest>
我需要在特定位置的新行中添加“Hello World”,最終檔案將如下所示
<?xml version="1.0" blablabla ..etc>
<uses-permission ~~~~~~ ..etc>
Hello World
<application ~~~some of words i dont know it and changes from file to another file~~~ >
Hello World
</application>
</manifest>
請問,我怎樣才能在 C# 中做到這一點?并且我不知道行中的一些單詞我將在其后添加“Hello World”,因此我需要先檢測該行,例如第 195 行中的“<application”,然后在 195 之后添加一個新行,它將是 196,然后添加單詞“你好世界”
uj5u.com熱心網友回復:
- 您想要操作 Android 應用清單 XML 檔案。
- 該格式記錄在 Android 開發者網站上。不幸的是,似乎沒有可用的 XSD 架構,但對于不需要的微不足道的更改。
- 為正確的作業使用正確的工具。
- 不要將 XML 檔案作為文本處理:
- 我可以從你最初的帖子中看出,這就是你的想法:
在它之后,所以我需要先檢測行,例如“
<application在第 195 行,然后在 195 之后添加一個新行,它將是 196,然后添加單詞"Hello World"問題是,XML 不關心行號:兩個語意相同的 XML 檔案可以有在最尷尬的地方,換行符的分布截然不同。 - 請記住:XML 檔案不是純文本檔案。
- XML 檔案(以及相關的,例如 SGML 和 HTML)的元素不能用正則運算式可靠地決議和提取。
- 這是因為XML 的語法是一種背景關系無關語法,它將它置于 Chomsky 層次結構中的正則語法 之上(因此,您不能使用正則運算式,QED)。
- 我可以從你最初的帖子中看出,這就是你的想法:
- 這意味著您應該改用 XML 處理庫。幸運的是 .NET至少有兩個:
System.Xml- 這是 .NET Framework 1.x 天(2000-2002)中較舊的 XML 庫。它以 W3C DOM 的早期標準為模型,并且非常難以使用。System.Xml.Linq(又名“ Linq to XML”)——這是一個現代(相對而言)的庫,它出現在 .NET Framework 3.5 中(與 Linq 以及我們今天仍然喜歡的各種好東西同時出現)。API 設計非常不同,但與System.Xml. 我注意到它主要是為了查詢 XML 而不是改變 XML 而設計的,但對于我們的目的來說它很好。- 首先閱讀:https : //docs.microsoft.com/en-us/dotnet/standard/linq/add-elements-attributes-nodes-xml-tree
- 不要將 XML 檔案作為文本處理:
雖然(一般來說),Android 清單檔案中元素的順序并不重要,但有 2 個記錄的例外順序很重要,不幸的是,對我們來說,第二個例外適用于我們:
- 一個
<activity-alias>元素必須跟在<activity>它作為別名的后面。 - 該
<application>元素必須是里面的最后一個元素<manifest>的元素。
所以鳥瞰圖是:
- 將 Android 應用清單 XML 檔案加載到
XDocument物件中。 - 要添加新
<uses-permission>元素或新的<permission>元素,你需要添加為根的直接子<manifest>元素,但位于之前的<application>元素。 - 要添加新
<service>元素,請將其附加<application>為新的直接子元素。
像這樣:
using System.Linq;
using System.Xml.Linq;
public static class Program
{
const String FILE_NAME = "MyManifest.xml";
public static async Task<Int32> Main( String[] args )
{
if( !File.Exists( FILE_NAME ) )
{
Console.WriteLine( "Couldn't find \"" FILE_NAME "\"." );
return 1;
}
String manifestXmlText = await File.ReadAllTextAsync( FILE_NAME );
XDocument manifestXml = XDocument.Parse( manifestXmlText );
XElement applicationEl = manifestXml.Descendants("application"); // Get the <application> element.
XNamespace? androidNS = manifestEl.GetNamespaceOfPrefix(prefix: "android"); // "http://schemas.android.com/apk/res/android"
if( androidNS is null ) throw new InvalidOperationException( "Couldn't find xmlns:android" );
{
// <uses-permission>'s attributes use XML namespaces, so this complicates things:
// https://stackoverflow.com/questions/4985974/xelement-namespaces-how-to
// https://docs.microsoft.com/en-us/dotnet/standard/linq/namespaces-overview
// https://stackoverflow.com/questions/2874422/how-to-set-the-default-xml-namespace-for-an-xdocument
// https://stackoverflow.com/questions/1338517/how-can-i-write-xml-with-a-namespace-and-prefix-with-xelement
XElement newUsesPermissionEl = new XElement( "uses-permission",
new XAttribute( androidNS "name" , "bar"),
new XAttribute( androidNS "maxSdkVersion", "123")
);
appEl.AddBeforeSelf( newUsesPermissionEl ); // Add the new <uses-permission> element as a preceding sibling of <application>.
}
{
// <service>:
XElement newServiceEl = new XElement( "service",
new XAttribute( androidNS "description", "Some day, some day, Some day, Dominion; Some day, some day, Some say prayers; I say mine"),
new XAttribute( androidNS "name" , "Dominion service"),
new XAttribute( androidNS "label" , "Andrew Eldritch fanclub <3"),
);
appEl.Add( newServiceEl ); // Add the new <service> element as an immediately child of <application>.
}
// Then save the XML document:
using( FileStream fs = File.OpenWrite( FILE_NAME ) )
{
await manifestXml.SaveAsync( fs );
}
return 0;
}
}
因此,將官方示例 Manifest XML作為輸入 (in MyManifest.xml)...:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.example.myapp">
<!-- Beware that these values are overridden by the build.gradle file -->
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="26" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<!-- This name is resolved to com.example.myapp.MainActivity
based upon the package attribute -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity" android:parentActivityName=".MainActivity" />
</application>
</manifest>
...我得到這個輸出:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.example.myapp">
<!-- Beware that these values are overridden by the build.gradle file -->
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="26" />
<uses-permission android:name="bar" android:maxSdkVersion="123" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<!-- This name is resolved to com.example.myapp.MainActivity
based upon the package attribute -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity" android:parentActivityName=".MainActivity" />
<service android:description="Some day, some day, Some day, Dominion; Some day, some day, Some say prayers; I say mine" android:name="Dominion service" android:label="Andrew Eldritch fanclub <3" />
</application>
</manifest>
如您所見,<service>和<uses-permission>元素已插入到正確的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389969.html
標籤:C#
上一篇:C#泛型創建新實體
下一篇:C#獲取所有可能的組合
