简体   繁体   中英

Parsing XML in C# creating a lists of user ID's

I am trying to create a list of user id's from an xml document in C#. Here is my latest try:

        string xml = WebexAutomation.LstSummaryUser();
        XDocument doc = XDocument.Parse(xml)
        var result = doc.Descendants("webExId").Single().Value;
        Console.WriteLine(result);

My XML looks like it has multiple users and I am trying to create list of each webExId

<?xml version="1.0" encoding="ISO-8859-1"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:use="http://www.webex.com/schemas/2002/06/service/user">
<serv:header>
    <serv:response>
        <serv:result>SUCCESS</serv:result>
        <serv:gsbStatus>PRIMARY</serv:gsbStatus>
    </serv:response>
</serv:header>
<serv:body>
    <serv:bodyContent xsi:type="use:lstsummaryUserResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <use:user>
            <use:webExId>kkkk</use:webExId>
            <use:firstName>kkkk</use:firstName>
            <use:lastName>llllll</use:lastName>
            <use:email>kkkkkk</use:email>
            <use:registrationDate>06/07/2014 01:17:13</use:registrationDate>
            <use:active>DEACTIVATED</use:active>
            <use:timeZoneID>11</use:timeZoneID>
        </use:user>
<use:user>
            <use:webExId>jjjj</use:webExId>
            <use:firstName>ssss</use:firstName>
            <use:lastName>dddd</use:lastName>
            <use:email>dfdfdf</use:email>
            <use:registrationDate>06/10/2013 12:23:52</use:registrationDate>
            <use:active>DEACTIVATED</use:active>
            <use:timeZoneID>11</use:timeZoneID>
        </use:user>
<use:matchingRecords>
            <serv:total>44</serv:total>
            <serv:returned>10</serv:returned>
            <serv:startFrom>1</serv:startFrom>
        </use:matchingRecords>
    </serv:bodyContent>
  </serv:body>
</serv:message>

Any suggestions?

Try this instead

var webExId = XName.Get("webExId", "http://www.tempuri.org");
var webExIds = doc.Descendants(webExId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM