-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..

9
  • 1
    XML is not valid since the namespace ns1 is not defined. Commented Aug 31, 2023 at 19:59
  • This is the soap response without envelope and header. How should the ns1 be defined? Commented Aug 31, 2023 at 20:13
  • @jdweng How should the ns1 be defined? Commented Aug 31, 2023 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. Commented Aug 31, 2023 at 20:39
  • You asked for and retrieved a string. Why would you expect InnerText to be an array or list? Microsoft's documentation has examples of navigating the tree. That seems like a good start... Commented Aug 31, 2023 at 21:39

1 Answer 1

1

it is always easier to use Linq

string[] test = XElement.Parse(response)
                            .Descendants("InfoResponse")
                            .Elements()
                            .Select(d => d.Value)
                            .ToArray();
Sign up to request clarification or add additional context in comments.

9 Comments

The code will not work without defining the namespace.
@jdweng I never post untested code
@jdweng - I have also tested this code and can confirm it works. The Elements() method doesn't care about namespace.
@Serge I tried both and both add 2 items, but one is empty and the second one has both values.. I edited my question and added your solution.
@user805528 You have significantly changed your xml. See my update
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.