尝试删除节点的xmlns名称空间

时间:2017-05-23 07:34:39

标签: c# xml

我对xml消息和c#有些问题。 问题是没有名称空间的根元素,并且所有名称空间都在节点中。

我已经运行了部分脚本来删除命名空间,因此我可以读取将发送到网络服务器的所有xml消息。

提出问题的消息:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetOrderResponseRequest xmlns="http://www.edibulb.nl/XML/Order:2">
            <Header>
                <UserName xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2">FBT_000390</UserName>
                <Password xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2">1FWcgwrx9</Password>
                <MessageID xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2" schemeDataURI="8719604082016">8719604082016100376</MessageID>
                <MessageDateTime xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2" format="304">20170523090413+02:00</MessageDateTime>
            </Header>
            <Body>
                <AgentParty>
                    <PrimaryID xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2" schemeID="251" schemeAgencyName="EBC">8719604178115</PrimaryID>
                </AgentParty>
                <GetOrderResponseDetails>
                    <MutationDateTime xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2" format="304">20170510000000+02:00</MutationDateTime>
                    <BuyerParty xmlns="urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2">
                        <PrimaryID xmlns="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:3" schemeID="251" schemeAgencyName="EBC">8719604082016</PrimaryID>
                    </BuyerParty>
                </GetOrderResponseDetails>
            </Body>
        </GetOrderResponseRequest>
    </soap:Body>
</soap:Envelope>

以下是要在webservice上翻译的脚本部分

如果涉及前缀,则下面的代码完全正常。 但它不适用于上面定义的xml。

这是我从网络服务中调用的类。

首先,我检查xml字符串是否有任何前缀。

RemoveNamespace remove = new RemoveNamespace();
public string orderrequest(string xmldoc, string ivbglns, bool success)
        {
            if (success == true)
            {
                if (xmldoc.Contains(":UserName"))
                {
                    string xdoc = remove.removeall(xmldoc);
                    docx = new XmlDocument();
                    docx.LoadXml(xdoc);
                }
                else if(xmldoc.Contains("<UserName xmlns"))
                {
                    string xdoc = remove.removexlmns(xmldoc);
                    docx = new XmlDocument();
                    docx.LoadXml(xmldoc);
                }
                 // rest of the code for the response 
             }
         }

并在RemoveNameSpace部分下面:

        public string removeall(string xdoc)
        {
            string docx = RemoveAllNamespaces(xdoc);
            return docx;
        }
        public static string RemoveAllNamespaces(string xmldoc)
        {
            XElement documentwithoutns = XRemoveAllNamespaces(XElement.Parse(xmldoc));

            return documentwithoutns.ToString();
        }
        private static XElement XRemoveAllNamespaces(XElement Xmldoc)
        {
            if (!Xmldoc.HasElements)
            {
                XElement element = new XElement(Xmldoc.Name.LocalName);
                element.Value = Xmldoc.Value;
                foreach (XAttribute attribute in Xmldoc.Attributes())
                    element.Add(attribute);
                return element;
            }
            return new XElement(Xmldoc.Name.LocalName, Xmldoc.Elements().Select(el => XRemoveAllNamespaces(el)));
        }
        public string removexlmns(string xdoc)
        {
            string pattern = "\\s+xmlns\\s*(:\\w)?\\s*=\\s*\\\"(?<url>[^\\\"]*)\\\"";
            MatchCollection matchcol = Regex.Matches(xdoc, pattern);

            foreach (Match m in matchcol)
            {
                xdoc = xdoc.Replace(m.ToString(), "");
            }
            return xdoc; 
        }

它返回的错误是:无法从&#34;重新定义前缀&#34; to&#39; urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2&#39;在同一个开始元素标记内。

我正在为此寻找解决方案。上面的xml是一条超出我控制范围的消息。

与亲切的问候

斯蒂芬

1 个答案:

答案 0 :(得分:1)

我强烈建议您在此之后的任何XML处理中使用命名空间。别再试图删除它们了!

如果您必须删除它们,那么值得注意XElement.Name是可变的。您可以删除所有名称空间声明,并将所有名称设置为其本地名称。

var doc = XDocument.Parse(xml);

doc.Descendants()
    .Attributes()
    .Where(x => x.IsNamespaceDeclaration)
    .Remove();

foreach (var element in doc.Descendants())
{
    element.Name = element.Name.LocalName;
}

请参阅this fiddle了解演示。

相关问题