Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch XDocument using LINQ without knowing the namespace
    primarykey
    data
    text
    <p>Is there a way to search an XDocument without knowing the namespace? I have a process that logs all SOAP requests and encrypts the sensitive data. I want to find any elements based on name. Something like, give me all elements where the name is CreditCard. I don't care what the namespace is.</p> <p>My problem seems to be with LINQ and requiring a xml namespace.</p> <p>I have other processes that retrieve values from XML, but I know the namespace for these other process.</p> <pre><code>XDocument xDocument = XDocument.Load(@"C:\temp\Packet.xml"); XNamespace xNamespace = "http://CompanyName.AppName.Service.Contracts"; var elements = xDocument.Root .DescendantsAndSelf() .Elements() .Where(d =&gt; d.Name == xNamespace + "CreditCardNumber"); </code></pre> <p>I really want to have the ability to search xml without knowing about namespaces, something like this:</p> <pre><code>XDocument xDocument = XDocument.Load(@"C:\temp\Packet.xml"); var elements = xDocument.Root .DescendantsAndSelf() .Elements() .Where(d =&gt; d.Name == "CreditCardNumber") </code></pre> <p>This will not work because I don't know the namespace beforehand at compile time.</p> <p>How can this be done?</p> <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Request xmlns="http://CompanyName.AppName.Service.ContractA"&gt; &lt;Person&gt; &lt;CreditCardNumber&gt;83838&lt;/CreditCardNumber&gt; &lt;FirstName&gt;Tom&lt;/FirstName&gt; &lt;LastName&gt;Jackson&lt;/LastName&gt; &lt;/Person&gt; &lt;Person&gt; &lt;CreditCardNumber&gt;789875&lt;/CreditCardNumber&gt; &lt;FirstName&gt;Chris&lt;/FirstName&gt; &lt;LastName&gt;Smith&lt;/LastName&gt; &lt;/Person&gt; ... &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Request xmlns="http://CompanyName.AppName.Service.ContractsB"&gt; &lt;Transaction&gt; &lt;CreditCardNumber&gt;83838&lt;/CreditCardNumber&gt; &lt;TransactionID&gt;64588&lt;/FirstName&gt; &lt;/Transaction&gt; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload