0

I have an XML and I wanna get a specific nested element using XPath but it does not work. Maybe that is why tags include xmlns attributes. Any suggestions?

<DataPDU xmlns="urn:cma:stp:xsd:stp.1.0">
<Body>
    <AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
        <test1>
            <test2>
                <test3>
                    <test4>text</test4>
                </test3>
            </test2>
        </test1>
        <test5>
            <test6>
                <test7>
                    <test8>dummy text</test8>
                </test7>
            </test6>
        </test5>
        <test9>test</test9>
        <test10>p1212121</test10>
        <test11>147</test11>
        <test12>lorem</test12>
    </AppHdr>
    <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">
        <test13>
                <test14>
                        <test15>asap</test15>
                </test14>
        </test13>
    </Document>
</Body>

1
  • As well as the "canonical" answer for which this is marked as a duplicate, search for "XSLT default namespace" for many other answers to this question, Commented May 21, 2020 at 7:44

1 Answer 1

-1

Yes you'll have to define the namespace. Have a look at this -

How to get XML element with namespace

Or you can pass the xml as string and use regex to remove those attributes -

Regex.Replace(xmlString, @"(\sxmlns="".+"")", "")

Depending on your use case, you can use either.

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

5 Comments

This solution works as intended. Why the downvote?
It does not work.Compiler show an error
Then discuss. Can you show your code? What method are you using? What is the compiler error?
syntax error at this @"(xmlns=\".+\")", "" part
ah yes, c# regex is slightly different, requires the double quote. I have editted my answer with the right regex pattern.