0

I've got some problems with xml messages and c#. The problem is a root element with no namespaces and all the namespaces are in the nodes.

I've got a part of the script running to delete the namespaces so I can read all the xml messages that will be sent to the webserver.

The message that gives the problems:

<?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>

And here is the part of the script to translate on the webservice

the code below works perfectly fine if there are prefixes involved. but it wont work with the xml defined above.

Here is the class that I call from the webservice.

First I check in the xml string if there are any prefixes.

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 
             }
         }

and below the RemoveNameSpace part:

        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; 
        }

The error it returns is: The Prefix "cannot be redefined from" to 'urn:ebl:edibulb:xml:data:draft:ReusableAggregateBusinessInformationEntity:2' within the same start element tag.

I'm in search for a solution for this. The xml from above is a message thats beyond my control.

with Kind regards

Stephan

  • 1
    Don't modify XML with string operations. Learn to properly use namespaces, easier [with XDocument](https://stackoverflow.com/questions/16018434/query-xdocument-with-xmlns-attribute-namespace?rq=1). – H H May 23 '17 at 07:52

1 Answers1

1

I would very strongly suggest you use the namespaces in whatever XML processing you are doing after this. Stop trying to remove them!

If you must remove them, it's worth noting that XElement.Name is mutable. You can remove all the namespace declarations and set all the names to their local names.

var doc = XDocument.Parse(xml);

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

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

See this fiddle for a demo.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45