Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop through XML Nodes
    primarykey
    data
    text
    <p>I am pulling some data from a website in XML format and it works fine when there are not consecutive nodes, but I am having trouble figuring out how to loop through them when there is. The data that I get looks something like...</p> <pre><code> &lt;whmcsapi&gt; &lt;action&gt;gettickets&lt;/action&gt; &lt;numreturned&gt;1&lt;/numreturned&gt; &lt;tickets&gt; &lt;ticket&gt; &lt;tid&gt;557168&lt;/tid&gt; &lt;name&gt;&lt;![CDATA[Array]]&gt;&lt;/name&gt; &lt;subject&gt;&lt;![CDATA[Test Ticket]]&gt;&lt;/subject&gt; &lt;message&gt;&lt;![CDATA[This is a test ticket&gt; &lt;ticket&gt; &lt;ticket&gt; &lt;tid&gt;557168&lt;/tid&gt; &lt;name&gt;&lt;![CDATA[Array]]&gt;&lt;/name&gt; &lt;subject&gt;&lt;![CDATA[Test Ticket]]&gt;&lt;/subject&gt; &lt;message&gt;&lt;![CDATA[This is a test ticket&gt; &lt;ticket&gt; </code></pre> <p>How can I loop through and read the data from each node? My code right now is the following...</p> <pre><code> public List&lt;Ticket&gt; Get_Tickets() { Dictionary&lt;string, string&gt; args = new Dictionary&lt;string, string&gt;(); args.Add("status", "All Active Tickets"); string data = Get_Data("gettickets", args); XDocument doc = XDocument.Parse(data); //var support_tickets = doc.Descendants("ticket").Select(ticket =&gt; new List&lt;Ticket&gt; support_tickets = (from x in doc.Descendants("ticket") select new Ticket { ID = x.Element("id").Value, TicketID = x.Element("tid").Value, DeptID = x.Element("deptid").Value, UserID = x.Element("userid").Value, Name = x.Element("name").Value, Email = x.Element("email").Value, Subject = x.Element("subject").Value, Message = x.Element("message").Value, }).ToList(); return support_tickets; } </code></pre> <p>The Ticket class...</p> <pre><code>public class Ticket { public string ID; public string TicketID; public string DeptID; public string UserID; public string Name; public string Email; public string CC; public string Subject; public string Message; public string Status; public string Priority; public DateTime Date; public DateTime LastResponse; public IPAddress IP; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload