0

I have some XML that has a namespace..

<batch xmlns="http://www.mydomain.uk/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="applications.xsd">
    <header>
        ...
    </header>
    <applications>
        <application>
            <details>
                <Title>MR</Title>
                <Forename>Jonathh</Forename>
                <Middlenames>
                    <Middlename>on</Middlename>
                    <Middlename>ath</Middlename>
                </Middlenames>
                <PresentSurname>H</PresentSurname>
            </details>
        </application>
    </applications>
</batch>

I am need to pull out the applications.. which I am able to do - but only by specifying the namespace..

var namespaceManager = new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("eb", "http://www.mydomain.uk/batch");

var protectedElement = doc.SelectSingleNode("/eb:batch/eb:applications", namespaceManager);

The problem for me is that the namespace then propagates down to the selected node E.G.

<applications xmlns="http://www.mydomain.uk/batch">
    <application>
        <details>
            <Title>MR</Title>
            <Forename>Jonathh</Forename>
            <Middlenames>
                <Middlename>on</Middlename>
                <Middlename>ath</Middlename>
            </Middlenames>
            <PresentSurname>H</PresentSurname>
        </details>
    </application>
</applications>

Note the sneaky little xmlns in the applications tag now.. that wasn't there before.. I've tried all sorts of things to remove..

I am so desperate I am considering a regular expression instead (I know!) - which I actually don't think would be too awful here....

Anyone got any suggestions?

Jon H
  • 1,061
  • 4
  • 13
  • 32
  • XPath has a `local-name()` function that can be used to match while ignoring namespaces, e.g. `*[local-name()='myElement']`. This will still not remove namespaces from the document in any way; if you need that (for some reason) you'll have to add more details on what you're doing with the node. – Jeroen Mostert Jul 01 '19 at 14:02
  • Thanks - I'd come across that in my travels if I recall correctly the namespace was still propergated to the selected node.. and I don't want that. I am trying to select, then cannocolise for token generation. I know that if I can get the selection right the rest of the process works fine.. – Jon H Jul 01 '19 at 16:43
  • [C# - How to remove xmlns from XElement](https://stackoverflow.com/q/40517306), [How to remove xmlns attribute from XDocument?](https://stackoverflow.com/q/7003983), [Search XDocument using LINQ without knowing the namespace](https://stackoverflow.com/q/2610947) or [How do I use XPath with a default namespace with no prefix?](https://stackoverflow.com/q/2524804) might answer your question, but without any XML or code we have no way to know you concrete problem. Can you share a [mcve] please? – dbc Jul 01 '19 at 18:34
  • And finally see [how to ignore namespaces with XPath](https://stackoverflow.com/q/4440451) and/or [How to remove all namespaces from XML with C#?](https://stackoverflow.com/q/987135). – dbc Jul 01 '19 at 19:33
  • JeroenMostert dbc I have edited the question to hopefully make it clearer.. i am trying to do a select using a namespace but then not have that namespace propagated down. – Jon H Jul 02 '19 at 10:09
  • @dbc hi be great to get some input now... – Jon H Jul 05 '19 at 19:14
  • @JonH - the `xmlns="http://www.mydomain.uk/batch"` doesn't "propagate" to the `` node, it was always there **implicitly**. `xmlns="http://www.mydomain.uk/batch"` on the root node implies that all child elements are also in that namespace, so when you select `` and examine it -- there it is. If you don't want that you need to strip the namespace from the `` node. If you are willing to switch to LINQ to XML I already linked to several solutions for doing that -- do those work for you? – dbc Jul 05 '19 at 19:34

0 Answers0