-2

I know there are a lot of the same questions but I can't crack this one. I have an XMLDocument and I need to iterate through the values.

        <InfoResponse xmlns="">
           <ns1:ID xmlns:ns1="http://www.svt.si/evlog">2399237000/03-069-1-41/2023</ns1:ID>
           <ns1:ID xmlns:ns1="http://www.svt.si/evlog">2399237000/03-069-1-42/2023</ns1:ID>
        </InfoResponse>

I have tried with IEnumerable, XmlNodelist, List and always get the same result.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(response);

String test = xmlDocument.InnerText;

returns

2399237000/03-069-1-41/20232399237000/03-069-1-42/2023

I need an array or a list so I can iterate through and perform next action.

I tried the suggested solutions How do I read and parse an XML file in C#? but the result is the same. I think the problem is that they all have the same node name/id.

Edit As suggested the namespace ns1 might not be defined so here is the whole string response I get:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
    <soap:Body>
        <InfoResponse xmlns="">
            <ns1:ID xmlns:ns1="http://www.svt.si/evlog">2399237000/03-069-1-41/2023</ns1:ID>
            <ns1:ID xmlns:ns1="http://www.svt.si/evlog">2399237000/03-069-1-42/2023</ns1:ID>
        </InfoResponse>
    </soap:Body>
</soap:Envelope>

Is the response ok?

I'm adding the suggested solution with the foreach - at this point I'm not sure, if I'm doing it correctly:

string[] strArray = XElement.Parse(response)
                        .Elements()
                        .Select(d => d.Value)
                        .ToArray();

foreach (string xn in strArray)
{
    Logger.LogInfo("ID: " + xn);
}

This now returns 2 elements - One empty and the other has both values.

Edit2

        string[] strArray = XElement.Parse(soapResponse)
                                    .Descendants("InfoResponse")
                                    .Elements()
                                    .Select(d => d.Value)
                                    .ToArray();

returns an empty array..

user805528
  • 189
  • 1
  • 5
  • 17
  • 1
    XML is not valid since the namespace ns1 is not defined. – jdweng Aug 31 '23 at 19:59
  • This is the soap response without envelope and header. How should the ns1 be defined? – user805528 Aug 31 '23 at 20:13
  • @jdweng How should the ns1 be defined? – user805528 Aug 31 '23 at 20:30
  • The ns1 must be in the file. To parse you probably need the envelope and header so the namespace is defined. Most libraries will give exceptions without the namespace. Post code with Envelope and Header and will give a solution. – jdweng Aug 31 '23 at 20:39
  • You asked for and retrieved a string. Why would you expect `InnerText` to be an array or list? [Microsoft's documentation](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument?view=net-7.0#navigate-the-document-tree) has examples of navigating the tree. That seems like a good start... – Heretic Monkey Aug 31 '23 at 21:39
  • Does this answer your question? [Using XmlDocument to Retrieve values in c#](https://stackoverflow.com/questions/17396655/using-xmldocument-to-retrieve-values-in-c-sharp) – Heretic Monkey Aug 31 '23 at 21:41
  • You get this envelope from some service, right? Does this service provide its description in the form of a wsdl? If so, generate a set of C# classes based on it and work with them. Then forget about raw XML and enjoy strict typing. – Alexander Petrov Sep 01 '23 at 08:57
  • @AlexanderPetrov yes there is a wsdl. It builds the classes,.. The problem is that it fails on SSL handshake. There seems to be a problem with the server certificate. So we decided on raw approach - time pressure :) – user805528 Sep 01 '23 at 14:18
  • @HereticMonkey I didn't expect an array out of what I wrote. I tried all options I found on stack and elswhere - just didn't have a working code :) – user805528 Sep 01 '23 at 14:51

1 Answers1

0

it is always easier to use Linq

string[] test = XElement.Parse(response)
                            .Descendants("InfoResponse")
                            .Elements()
                            .Select(d => d.Value)
                            .ToArray();
Serge
  • 40,935
  • 4
  • 18
  • 45