我有以下xml文件,似乎无法弄清楚如何获取元素中的值(它们埋在CDATA中)。我正在尝试将linq用于xml。如果有人知道如何将其转换为“Product”对象(假设我们有一个产品对象具有与元素同名的属性)。提前谢谢。
鲍勃
<CNETResponse realm="cnet" version="1.0" xmlns="http://api.cnet.com/rest/v1.0/ns" xmlns:xlink="http://www.w3.org/1999/xlink">
<TechProduct id="33517677">
<Name><![CDATA[Nikon CoolPix L20 (deep red)]]></Name>
<Topic id="1670"></Topic>
<ImageURL width="60"><![CDATA[http://i.i.com.com/cnwk.1d/sc/33517677-2-60-0.gif]]></ImageURL>
</TechProduct>
</CNETResponse>
答案 0 :(得分:2)
问题是名称空间 - 例如:
XNamespace ns = "http://api.cnet.com/rest/v1.0/ns";
XElement techProd = doc.Root.Element(ns + "TechProduct");
Product product = new Product {
Id = (int)techProd.Attribute("id"),
Name = techProd.Element(ns + "Name").Value,
Topic = techProd.Element(ns + "Topic").Value,
TopicId = (int)techProd.Element(ns + "Topic").Attribute("id"),
ImageUrl = techProd.Element(ns + "ImageURL").Value,
ImageWidth = (int)techProd.Element(ns + "ImageURL").Attribute("width"),
};
您可能也更喜欢XmlSerializer
- 类似于:
XmlSerializer ser = new XmlSerializer(typeof(CnetResponse));
CnetResponse response = (CnetResponse)ser.Deserialize(new StringReader(xml));
TechProduct product = response.TechProduct;
使用类定义:
[Serializable, XmlRoot("CNETResponse", Namespace = CnetResponse.Namespace)]
public class CnetResponse {
public const string Namespace = "http://api.cnet.com/rest/v1.0/ns";
public TechProduct TechProduct { get; set; }
}
[Serializable, XmlType(Namespace = CnetResponse.Namespace)]
public class TechProduct
{
[XmlAttribute("id")]
public int Id { get; set; }
public string Name {get;set;}
public Topic Topic { get; set; }
[XmlElement("ImageURL")]
public Image Image { get; set; }
}
[Serializable, XmlType(Namespace = CnetResponse.Namespace)]
public class Topic {
[XmlAttribute("id")]
public int Id { get; set; }
[XmlText]
public string Text {get;set;}
}
[Serializable, XmlType(Namespace = CnetResponse.Namespace)]
public class Image {
[XmlAttribute("width")]
public int Width { get; set; }
[XmlText]
public string Url {get;set;}
}
或者,只需通过xsd.exe
运行xml以获得适合的C#代码:
xsd foo.xml
xsd foo.xsd /classes
答案 1 :(得分:1)
假设您的Product
类具有此处使用的构造函数,请尝试此操作,其中rawXml
是CNET响应XML:
XElement cnetResponse = XElement.Parse(rawXml);
IEnumerable<NameQty> products =
from e in cnetResponse.Descendants("TechProduct")
select new Product(
(string)e.Element("Name"),
(int)e.Element("Topic").Attribute("id"),
(string)e.Element("ImageURL")
);
foreach(Product p in products)
{
// do stuff
}
我无法访问要对其进行测试的计算机,因此我不做任何保修。