• BizTalk 新增/修改/删除 XmlDocument 名字空间的高效方法


    新增一个名字空间

    public class AddXmlNamespaceStream : XmlTranslatorStream
    {
        private String namespace_;
        private int level_ = 0; // hierarchy level
    
        public AddXmlNamespaceStream(Stream stream, String @namespace)
            : base(XmlReader.Create(stream))
        {
            namespace_ = @namespace;
        }
    
        #region XmlTranslatorStream Overrides
    
        protected override void TranslateStartElement(string prefix, string localName, string nsURI)
        {
            if (level_++ != 0)
            {
                base.TranslateStartElement(prefix, localName, nsURI);
                return;
            }
    
            if (String.IsNullOrEmpty(nsURI))
            {
                nsURI = namespace_;
                if (String.IsNullOrEmpty(prefix))
                    prefix = "__bts_ns0__";
            }
    
            base.TranslateStartElement(prefix, localName, nsURI);
        }
    
        protected override void TranslateEndElement(bool full)
        {
            if (level_-- != 0)
            {
                base.TranslateEndElement(full);
                return;
            }
    
            base.TranslateEndElement(full);
        }
    
        #endregion
    }
    

      删除名字空间

    public class XmlNamespaceRemoverStream : XmlTranslatorStream
    {
        protected override void TranslateStartElement(
            string prefix, string localName, string nsURI)
        {
            base.TranslateStartElement(null, localName, null);
        }
    
        protected override void TranslateAttribute()
        {
            if (this.m_reader.Prefix != "xmlns")
                base.TranslateAttribute();
        }
    
        public XmlNamespaceRemoverStream(Stream input)
            : base(new XmlTextReader(input), Encoding.Default)
        { }
    }
    

      修改根节点 名字空间

    public class XmlNamespaceModifierStream : XmlTranslatorStream
        {
            private const string CIN_DOCTYPE = "catalogueItemNotification";
            private const string CPN_DOCTYPE = "priceSynchronisationDocument";
            private const string ROOT_GS1_ELEMENT = "StandardBusinessDocument";
            private const string NS_PREFIX = "http://www.test.com";
    
            private string _newNamespaceVersion;
            private string _documentType;
    
    
    
    
            protected override void TranslateStartElement(string prefix, string localName, string nsURI)
            {
                string newNSUri = string.Empty;
                bool isElementFoundWithNamespace = false;
                bool isFirstElement = false;
    
    
                if (!string.IsNullOrEmpty(prefix) && !isFirstElement)
                {
                    //element found with prefix. Modify namespace with new value and append passed namespace version 
                    newNSUri = NS_PREFIX + _newNamespaceVersion;
                    isElementFoundWithNamespace = true;
    
                    if (localName.Equals(ROOT_GS1_ELEMENT))
                        isFirstElement = true;
                }
    
    
                if (isElementFoundWithNamespace & isFirstElement)
                {
                    //replace with new namespace
                    Debug.WriteLine(string.Format("[BTS.Utilities.CustomPipelines.NamespaceModifier.XmlNamespaceModifierStream]Replace namespace with {0}", nsURI + newNSUri));
    
                    if (_documentType.Equals(CIN_DOCTYPE))
                        localName = localName + "Catalogue";
                    if (_documentType.Equals(CPN_DOCTYPE))
                        localName = localName + "Price";
    
                    base.TranslateStartElement(null, localName, newNSUri);
                    //base.TranslateStartElement(prefix, localName, newNSUri);
                }
                else
                {
                    base.TranslateStartElement(null, localName, null);
                    //base.TranslateStartElement(prefix, localName, newNSUri);
                }
    
            }
    
            protected override void TranslateAttribute()
            {
                if (this.m_reader.Prefix != "xmlns" && this.m_reader.Name != "xmlns")
                    base.TranslateAttribute();
            }
    
    
    
            public XmlNamespaceModifierStream(Stream input, string namespaceVersion, string documentType)
                : base(new XmlTextReader(input), Encoding.Default)
            {
                Debug.WriteLine("[BTS.Utilities.CustomPipelines.NamespaceModifier.XmlNamespaceModifierStream]Entered method");
                _newNamespaceVersion = namespaceVersion.Trim();
                _documentType = documentType.Trim();
                Debug.WriteLine("[BTS.Utilities.CustomPipelines.NamespaceModifier.XmlNamespaceModifierStream]Exit method");
            }
        }
    

      

  • 相关阅读:
    tomcat 调优-生产环境必备
    Spring中 BeanFactory和ApplicationContext的区别
    延迟队列DelayQueue简单入门
    RedisTemplate中opsForValue的使用 (复制粘贴的,没有练习,找时间回顾 )
    yum安装nginx的默认目录详解
    Centos 6无法使用yum解决办法
    JVM:jmap heap 堆参数分析MinHeapFreeRatio、MaxHeapFreeRatio、MaxHeapSize、NewSize、MaxNewSize
    maven下载Oracle jar包
    idea查看源代码出现/* compiled code */
    今日进度
  • 原文地址:https://www.cnblogs.com/neozhu/p/3245116.html
Copyright © 2020-2023  润新知