簡體   English   中英

試圖刪除節點的xmlns命名空間

[英]trying to delete xmlns namespaces of nodes

我在xml消息和C#中遇到了一些問題。 問題是沒有名稱空間的根元素,並且所有名稱空間都在節點中。

我已經運行了一部分腳本來刪除名稱空間,以便可以讀取將發送到Web服務器的所有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>

這是腳本的一部分,可在網絡服務上進行翻譯

如果涉及前綴,則下面的代碼可以很好地工作。 但它不能與上面定義的xml一起使用。

這是我從Web服務調用的類。

首先,我檢查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; 
        }

它返回的錯誤是:在同一開始元素標記中,前綴“無法從定義為”到“ urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2”。

我正在尋找解決方案。 上面的xml是我無法控制的消息。

親切的問候

史蒂芬

我強烈建議您在此之后進行的任何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;
}

看到這個小提琴演示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM