Is there a way to search for XDocument without knowing the namespace? I have a process that logs all SOAP requests and encrypts sensitive data. I want to find any items based on the name. Something like, give me all the elements where the name is CreditCard. I don't care what a namespace is.
My problem seems to be related to LINQ and requires an xml namespace.
I have other processes that extract values ββfrom XML, but I know the namespace for this other process.
XDocument xDocument = XDocument.Load(@"C:\temp\Packet.xml"); XNamespace xNamespace = "http://CompanyName.AppName.Service.Contracts"; var elements = xDocument.Root .DescendantsAndSelf() .Elements() .Where(d => d.Name == xNamespace + "CreditCardNumber");
I really want to be able to search for xml without knowing about namespaces, something like this:
XDocument xDocument = XDocument.Load(@"C:\temp\Packet.xml"); var elements = xDocument.Root .DescendantsAndSelf() .Elements() .Where(d => d.Name == "CreditCardNumber")
This will not work because I do not know the namespace in advance at compile time.
How can I do that?
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Request xmlns="http://CompanyName.AppName.Service.ContractA"> <Person> <CreditCardNumber>83838</CreditCardNumber> <FirstName>Tom</FirstName> <LastName>Jackson</LastName> </Person> <Person> <CreditCardNumber>789875</CreditCardNumber> <FirstName>Chris</FirstName> <LastName>Smith</LastName> </Person> ... <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Request xmlns="http://CompanyName.AppName.Service.ContractsB"> <Transaction> <CreditCardNumber>83838</CreditCardNumber> <TransactionID>64588</FirstName> </Transaction> ...
c # linq-to-xml
Mike Barlow - BarDev Apr 09 '10 at 21:09 2010-04-09 21:09
source share