新增一个名字空间
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"); } }