0

我将一个XML字符串作为 Web 服务发送,我想解释它。我的 XML 字符串是这样的:

<?xml version="1.0" encoding="utf-8"?>
<result is_array="true">
    <item>
        <candidate_offer_id>175</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
        </item>
        <item><candidate_offer_id>176</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
    </item>
</result>

在这个 XML 字符串中,我想访问列表中节点 <item> 下的子节点(如候选者_offer_id、offer_status 名称),以便稍后我可以运行循环以在循环中获取所有这些值并将其放在 Excel 工作表上。到现在为止我是这样写的:

WebResponse response = request.GetResponse();

Stream responseStream = response.GetResponseStream();

// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(responseStream);

string responseFromServer = reader.ReadToEnd();

StringBuilder output = new StringBuilder();
var str = XElement.Parse(responseFromServer);
var result = str.Element("item");

但是我如何解释var结果以获取列表中父节点 <item> 的标签名称以及如何解释它?

4

3 回答 3

1

您还可以使用XmlDocument类来加载和操作 XML。

XmlDocument doc = new XmlDocument();
//doc.Load(responseStream); -- Stream loading
doc.LoadXml(responseFromServer); // where responseFromServer is a xml string

XmlNodeList list = doc.DocumentElement.SelectNodes("//item/*");

foreach (XmlNode n in list)
{
    Console.WriteLine("{0} : {1}", n.Name, n.Value);
}

// As a list object that can be converted further
IEnumerable<XmlNode> node = list.Cast<XmlNode>();

我假设<code>标签有误;如果没有,xml 将需要更正才能使用。

*更新:简化将子节点提取为列表的代码

于 2013-09-26T12:18:10.297 回答
0

首先,您的 XML 无效。你想要<code>之后<?xml>喜欢:

<?xml version=""1.0"" encoding=""utf-8""?>
<code>
    <result is_array=""true"">
        <item>
            <candidate_offer_id>175</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
        </item>
        <item><candidate_offer_id>176</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
        </item>
    </result>
</code>

我会创建一个类来包含每个项目,比如

static void Main(string[] args)
{
    var str = XElement.Parse(xml);
    var items = str.Descendants("item");

    List<Item> Items = new List<Item>();

    foreach (var item in items)
    {
        Items.Add(new Item
        {
            OfferID = Convert.ToInt32(item.Element("candidate_offer_id").Value),
            Person = item.Element("contact_person").Value,
            Status = Convert.ToInt32(item.Element("offer_status").Value)
        });
    }
}

class Item
{
    public int OfferID { get; set; }
    public string Person { get; set; }
    public int Status { get; set; }
}
于 2013-09-26T12:09:09.297 回答
0

XDocument将适用于这种情况,因为您没有使用整个 XML 结构。

假设您的 XML 是有效的,例如:

<?xml version="1.0" encoding="UTF-8"?>
<result is_array="true">
    <item>
        <candidate_offer_id>175</candidate_offer_id>
        <contact_person>Ranjeet Singh</contact_person>
        <offer_status>8</offer_status>
    </item>
    <item>
        <candidate_offer_id>176</candidate_offer_id>
        <contact_person>Ranjeet Singh</contact_person>
        <offer_status>8</offer_status>
    </item>
</result>

DTO:

public class CandidateOffer
{
    public int CandidateOfferId { get; set; }
    public string ContactPerson { get; set; }
    public int OfferStatus { get; set; }
}

解析器:

public CandidateOffer ParseCandidateOffer(XElement element)
{
    int candidateOfferId;

    if(!int.TryParse(element.Element("candidate_offer_id").Value,
                     out candidateOfferId))
    {
        candidateOfferId = 0;
    }

    var contactPerson = element.Element("contact_person").Value;

    int offerStatus;

    if(!int.TryParse(element.Element("offer_status").Value,
                     out offerStatus))
    {
        offerStatus = 0;
    }

    return new CandidateOffer
            {
                CandidateOfferId = candidateOfferId,
                ContactPerson = contactPerson,
                OfferStatus = offerStatus
            };
}

用法:

var xDocument = XDocument.Parse(xmlString);

var candidateOffers = xDocument.XPathSelectElements("//item")
                               .Select(ParseCandidateOffer);

foreach(var candidateOffer in candidateOffers)
{
    Console.WriteLine(candidateOffer.CandidateOfferId);
}
于 2013-09-26T12:17:44.600 回答