[英]C# Adding attribute to XML element appends the namespace to the end of the element
Im using unity3d and I want to add some attributes the android manifest that unity generated, this is my code, modified from this comment https://stackoverflow.com/a/54894488/2126254 我正在使用unity3d,我想添加一些属性,即由unity生成的android清单,这是我的代码,从此注释中修改过https://stackoverflow.com/a/54894488/2126254
public class ModifyUnityAndroidAppManifest : IPostGenerateGradleAndroidProject
{
public void OnPostGenerateGradleAndroidProject(string basePath)
{
var androidManifest = new AndroidManifest(GetManifestPath(basePath));
XmlAttribute ReplaceBackupAttr = androidManifest.GenerateAttribute(
"tools", "replace", "android:allowBackup", androidManifest.ToolsXmlNamespace);
XmlAttribute AllowBackupAttr = androidManifest.GenerateAttribute(
"android", "allowBackup", "true", androidManifest.AndroidXmlNamespace);
androidManifest.SetAttribute(ReplaceBackupAttr);
androidManifest.SetAttribute(AllowBackupAttr);
androidManifest.Save();
}
public int callbackOrder { get { return 1; } }
private string _manifestFilePath;
private string GetManifestPath(string basePath)
{
... // irrelevnat
}
}
internal class AndroidXmlDocument : XmlDocument
{
private string m_Path;
protected XmlNamespaceManager nsMgr;
public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
public readonly string ToolsXmlNamespace = "http://schemas.android.com/apk/res/tools";
public AndroidXmlDocument(string path)
{
m_Path = path;
using (var reader = new XmlTextReader(m_Path))
{
reader.Read();
Load(reader);
}
nsMgr = new XmlNamespaceManager(NameTable);
nsMgr.AddNamespace("android", AndroidXmlNamespace);
nsMgr.AddNamespace("tools", ToolsXmlNamespace);
}
public string Save()
{
return SaveAs(m_Path);
}
public string SaveAs(string path)
{
using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
Save(writer);
}
return path;
}
}
internal class AndroidManifest : AndroidXmlDocument
{
internal readonly XmlElement ApplicationElement;
public AndroidManifest(string path) : base(path)
{
ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
}
internal XmlAttribute GenerateAttribute(string prefix, string key, string value, string XmlNamespace)
{
XmlAttribute attr = CreateAttribute(prefix, key, XmlNamespace);
attr.Value = value;
return attr;
}
internal void SetAttribute(XmlAttribute Attribute)
{
ApplicationElement.Attributes.Append(Attribute);
}
}
My issue is that after I add the 2 attributes (replace and allowBackup), the tools Namespace is also appended to the end of the tag 我的问题是,在添加2个属性(replace和allowBackup)之后,工具Namespace也附加到了标记的末尾
<application android:theme="@style/UnityThemeSelector" android:icon="@mipmap/app_icon" android:label="@string/app_name" android:isGame="true" android:banner="@drawable/app_banner" tools:replace="android:allowBackup" android:allowBackup="true" xmlns:tools="http://schemas.android.com/apk/res/tools">
How can I fix this? 我怎样才能解决这个问题? I tried setting XmlNamespace to null, but this causes the prefix (tools,android) not to be printed.
我尝试将XmlNamespace设置为null,但这导致不打印前缀(tools,android)。
Figured it out, I had " http://schemas.android.com/tools "; 想通了,我有“ http://schemas.android.com/tools ”; in the manifest tag, but " http://schemas.android.com/apk/res/tools ";
在清单标记中,但“ http://schemas.android.com/apk/res/tools ”; for the "tools" attribute, so the c# write added it the application tag.
的“工具”属性,因此c#编写将其添加了应用程序标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.