0

I have an XML as below

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
    xmlns="http://com/uhg/uht/uhtSoapMsg_V1"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header>
        <uhtHeader
            xmlns="http://com/uhg/uht/uhtHeader_V1">
            <consumer>COMET</consumer>
            <auditId></auditId>
            <sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
            <environment>P</environment>
            <businessService version="24">getClaimHistory</businessService>
            <status>success</status>
        </uhtHeader>
    </env:Header>
    <env:Body>
        <srvcRspn
            xmlns="http://com/uhg/uht/getClaimHistory_V24">
            <srvcErrList arrayType="srvcErrOccur[1]" type="Array">
                <srvcErrOccur>
                    <orig>Foundation</orig>
                    <rtnCd>00</rtnCd>
                    <explCd>000</explCd>
                    <desc></desc>
                </srvcErrOccur>
            </SrvcErrList>
        </srvcRspn>
    </env:Body>
</env:Envelope>

I want to remove all the attribute values with "http" like below:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
    xmlns=""
    xmlns:env="">
    <env:Header>
        <uhtHeader
            xmlns="">
            <consumer>COMET</consumer>
            <auditId></auditId>
            <sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
            <environment>P</environment>
            <businessService version="24">getClaimHistory</businessService>
            <status>success</status>
        </uhtHeader>
    </env:Header>
    <env:Body>
        <srvcRspn
            xmlns="">
            <srvcErrList arrayType="srvcErrOccur[1]" type="Array">
                <srvcErrOccur>
                    <orig>Foundation</orig>
                    <rtnCd>00</rtnCd>
                    <explCd>000</explCd>
                    <desc></desc>
                </srvcErrOccur>
            </SrvcErrList>
        </srvcRspn>
    </env:Body>
</env:Envelope>

I have tried several ways but none of them has worked for me. Can anyone suggest what is fastest way to do it in VB.NET/C#.

The actual response is very large (approx 100000 lines of XML minimum) and using for each will consume a good amount of time. Is there any parsing method or LINQ query method which can do it faster.

7
  • Both VB.NET and C# have exactly the same speed. The language used to access .net API means nothing to speed. Commented Sep 4, 2020 at 14:46
  • I can not use For Each to traverse each node and check, is there any faster method to do it is what I actually need Commented Sep 4, 2020 at 14:47
  • You do not need to remove XML attributes, you are trying to get rid of the XML namespaces. Commented Sep 4, 2020 at 14:51
  • Does this answer your question? How to remove all namespaces from XML with C#? Commented Sep 4, 2020 at 14:51
  • @Cleptus Thank for redirecting me to correct place. It worked!!! Commented Sep 4, 2020 at 15:02

1 Answer 1

1

I got the way to do it using Regex as below:

Return Regex.Replace(xmlDoc, "((?<=<|<\/)|(?<= ))[A-Za-z0-9]+:| xmlns(:[A-Za-z0-9]+)?="".*?""", "")

It serves my purpose completely. Thanks Cleptus for your quick reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.