I'm new to XML and am having trouble reading my XML response back from a HttpWebResponse.
Here is the response back:
<RESPONSE version="1.2">
<RESULTS>
<AN an_type="C"
an_id="783hdryfdg56a2"
an_num="1"
an_status="100" />
<RESULTS>
</RESPONSE>
I'm looking to extract out the an_id value and save it to a list. Started doing this but seems to get an for xmlnodelist as an int but it thinks nodes["an_id"] is a string
List<int> IDs = new List<int>();
XmlDocument doc = new XmlDocument();
doc.LoadXml(returnValue);
XmlNodeList nodes = doc.SelectNodes("SEARCH_RESULTS/LOAN");
LoanIDs.Add(Convert.ToInt32(nodes["an_id"].InnerText));
Also is their a way to once the an_id's are added to a list. foreach item in the list use it as aparameter for a new xml like this:
<INPUT>
<LOGIN API_ID=""cat"" API_PASSWORD=""dog"" />
<REQUEST>
<AN an_id=""@anID"" />
<AN an_id=""@anID"" />
....foreach one in list it adds a new node with the value
</REQUEST>
</INPUT>
With xml being your string with the XML code:
var ids = XDocument.Parse(xml).
Descendants("AN").
Select(e => (string)e.Attribute("an_id")).
ToList();
However, from your sample it didn't seem that the attribute is always an integer. Are you sure about your conversion? I changed the cast of the attribute to (string) given your latest comment.
When you want to reuse the list, with current being the parent element containing the ones you want to add:
current.Add(ids.Select(i => new XElement("AN", new XAttribute("an_id", i))));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With