在附加孩子时重复XMLNS

时间:2014-12-08 14:40:41

标签: c# xml xml-namespaces

我正在尝试从我项目的另一个模块中自动化JBoss-AS-7.1.1的standalone.xml。

以下是我试图更新的XML部分

</subsystem>
<subsystem xmlns="urn:jboss:domain:configadmin:1.0" />
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
  <datasources>
    <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
      <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
      <driver>h2</driver>
      <security>
        <user-name>sa</user-name>
        <password>sa</password>
      </security>
    </datasource>
    <drivers>
      <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
      </driver>
      <driver name="mysql" module="com.mysql.jdbc">
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
      </driver>
    </drivers>
   </datasources>
</subsystem>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">

正确更新后,它看起来像

<subsystem xmlns="urn:jboss:domain:configadmin:1.0" />
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
  <datasources>
    <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
      <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
      <driver>h2</driver>
      <security>
        <user-name>sa</user-name>
        <password>sa</password>
      </security>
    </datasource>
    <drivers>
      <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
      </driver>
      <driver name="mysql" module="com.mysql.jdbc">
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
      </driver>
    </drivers>
    <datasource jndi-name="java:/session-tracking-dataSource-orcl" pool-name="session-tracking-dataSource-orcl" enabled="true" use-java-context="true">
      <connection-url>jdbc:mysql://127.0.0.1:3306//fusion?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;</connection-url>
      <connection-property name="charSet">UTF-8</connection-property>
      <driver>mysql</driver>
      <pool>
        <max-pool-size>10</max-pool-size>
        <min-pool-size>1</min-pool-size>
      </pool>
      <security>
        <user-name>root</user-name>
        <password>root</password>
      </security>
      <timeout>
        <idle-timeout-minutes>1</idle-timeout-minutes>
        <query-timeout>5</query-timeout>
      </timeout>
    </datasource>
  </datasources>
</subsystem>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">

所以这是我的代码。

XMLObj.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace XMLOpsHelper
{
    class XMLObj
    {
        private string name;
        private List<Object> value;
        private Dictionary<string, string> attr;
        private XMLContext context;

        public XMLObj(string name, XMLContext context)
        {
            this.name = name;
            this.context = context;
            value = new List<Object>();
            attr = new Dictionary<string, string>();
        }

        public void addAttribute(string key, string value)
        {
            this.attr.Add(key, value);
        }

        public void assignValue(Object val)
        {
            if ((val.GetType() == typeof(String)) || (val.GetType() == typeof(string)) || (val.GetType() == typeof(XMLObj)))
            {
                this.value.Add(val);
            }
        }

        // for removing empty xmlns
        public Object _toString()
        {
            XmlNode newNode = context.Doc.CreateNode(XmlNodeType.Element, this.name, context.XmlNS);

            foreach (KeyValuePair<string, string> entry in this.attr)
            {
                XmlAttribute attr = context.Doc.CreateAttribute(entry.Key);
                attr.Value=entry.Value;
                newNode.Attributes.Append(attr);
            }

            foreach (Object item in this.value)
            {
                if (item is string)
                {
                    newNode.InnerText=(string)item;
                }

                if (item is XMLObj)
                {
                    Object nextItem = ((XMLObj)item)._toString();
                    if (nextItem is XmlNode)
                    {
                        newNode.AppendChild((XmlNode)nextItem);
                    }
                }
            }
            return newNode;
        }
    }
}

XMLObj类的背景知识:对于每个标记新实例,将创建此类。如果附加的innerXML / innerText将进入值列表。稍后在根对象上调用的_toString(临时名称)方法,以将其转换为标记。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace XMLOpsHelper
{
    class XMLUpdater
    {
        XmlDocument doc = new XmlDocument();
        XmlNode root;

        public XMLUpdater(string xmlStr) {
            doc.LoadXml(xmlStr);
        }

        public XMLUpdater(XmlNode xmlNode)
        {
            doc.LoadXml(xmlNode.OuterXml);
        }

        public XMLUpdater(XmlNode xmlNode, XmlNode root)
        {
            doc.LoadXml(xmlNode.OuterXml);
            this.root = root;
        }

        public void appendChild(XmlNode parentNode){
            XmlNode importNode = root.OwnerDocument.ImportNode(doc.DocumentElement, true);

            parentNode.AppendChild(importNode);
        }

        public XmlDocument getDoc() {
            return this.doc;
        }
    }
}

创建XMLObject层次结构并将其转换为标记XMLUpdater通过调用appendChild将其置于适当的位置。

最终结果是

<subsystem xmlns="urn:jboss:domain:configadmin:1.0" />
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
  <datasources>
    <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
      <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
      <driver>h2</driver>
      <security>
        <user-name>sa</user-name>
        <password>sa</password>
      </security>
    </datasource>
    <drivers>
      <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
      </driver>
      <driver name="mysql" module="com.mysql.jdbc">
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
      </driver>
    </drivers>
    <datasource jndi-name="java:/session-tracking-dataSource-orcl" pool-name="session-tracking-dataSource-orcl" enabled="true" use-java-context="true" xmlns="urn:jboss:domain:datasources:1.0">
      <connection-url>jdbc:mysql://127.0.0.1:3306//fusion?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;</connection-url>
      <connection-property name="charSet">UTF-8</connection-property>
      <driver>mysql</driver>
      <pool>
        <max-pool-size>10</max-pool-size>
        <min-pool-size>1</min-pool-size>
      </pool>
      <security>
        <user-name>root</user-name>
        <password>root</password>
      </security>
      <timeout>
        <idle-timeout-minutes>1</idle-timeout-minutes>
        <query-timeout>5</query-timeout>
      </timeout>
    </datasource>
  </datasources>
</subsystem>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">

XMLNS将附加在以下行的末尾,由于子系统已经拥有它,因此不会出现这种情况

<datasource jndi-name="java:/session-tracking-dataSource-orcl" pool-name="session-tracking-dataSource-orcl" enabled="true" use-java-context="true" xmlns="urn:jboss:domain:datasources:1.0">

知道我做错了吗?

0 个答案:

没有答案