When I have an XDocument and I know a namespace but not its prefix, and I want to get a certain descendant with prefixes and not namespaces (e.g. exactly as it was in the original XML string), how can I?
For example, suppose I have
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gdp="http://example.com/asdf">
<soapenv:Header/>
<soapenv:Body>
<gdp:GetLotNum/>
</soapenv:Body>
</soapenv:Envelope>
So perhaps I’d try:
XElement envelope = XElement.Parse(soapEnvelopeXml);
XNamespace envelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
return envelope.Descendants(envelopeNamespace + "Body").FirstOrDefault()?.ToString();
But that gives me full namespaces, resulting in:
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<gdp:GetLotNum xmlns:gdp="http://example.com/asdf" />
</soapenv:Body>
Whereas I want the exact XML I started with:
<soapenv:Body>
<gdp:GetLotNum/>
</soapenv:Body>
Can I just strip off the namespaces somehow? Or is there a more elegant way?