0

I am trying to enumerate this xml (sample):

<SearchSuggestion xmlns="http://opensearch.org/searchsuggest2" version="2.0">
   <Query xml:space="preserve">Relativity</Query>
   <Section>
    <Item>
        <Text xml:space="preserve">Relativity</Text>
        <Url xml:space="preserve">https://en.wikipedia.org/wiki/Relativity</Url>
    </Item>
    <Item>
        <Text xml:space="preserve">Relativity priority dispute</Text>
        <Url xml:space="preserve">https://en.wikipedia.org/wiki/Relativity_priority_dispute</Url>
    </Item>
....

using:

XmlNodeList nItems = rssDoc.SelectNodes("/SearchSuggestion/Section/Item")

But it is yielding no results. Can anyone tell me why?

Gary K
  • 29
  • 5
  • Maybe this question is related to yours? https://stackoverflow.com/questions/16889895/c-sharp-xmldocument-selectnodes-is-not-working – fluidguid Feb 21 '20 at 07:49
  • I strongly suspect it's due to the namespaces. It's fixable with XPath, but I'd personally suggest using LINQ to XML instead - that tends to be simpler in terms of namespace handling, IMO. – Jon Skeet Feb 21 '20 at 07:50
  • And, here is documentation from Microsoft, hope that helps. https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectnodes?view=netframework-4.8 – fluidguid Feb 21 '20 at 07:51
  • You have a default namespace `xmlns="http://opensearch.org/searchsuggest2"` so follow the instructions from [Using Xpath With Default Namespace in C#](https://stackoverflow.com/q/585812/3744182). – dbc Feb 21 '20 at 08:18
  • Thanks. The simple string replace worked a treat – Gary K Feb 21 '20 at 09:25

0 Answers0