C#向XML元素添加属性将名称空间附加到元素的末尾

时间:2019-07-04 11:30:04

标签: c# android .net xml unity3d

我正在使用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);
        }
    }

我的问题是,在添加2个属性(replace和allowBackup)之后,工具命名空间也被附加到了标记的末尾

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

我该如何解决? 我尝试将XmlNamespace设置为null,但这导致不打印前缀(工具,android)。

1 个答案:

答案 0 :(得分:0)

弄清楚,我有“ http://schemas.android.com/tools”;在清单标记中,但为“ http://schemas.android.com/apk/res/tools”;的“工具”属性,因此c#编写将其添加了应用程序标记。