更新时间:2023-10-01 22:25:52
按后代和元素搜索时,需要指定命名空间.使用 LINQ to XML 很容易做到这一点.看起来你快到了,但没有为元素这样做:
When you're searching by descendants and element, you need to specify the namespace. This is pretty easy with LINQ to XML. It looks like you were nearly there, but didn't do it for the elements:
XDocument xmlDoc = XDocument.Load(currentDir + "\Cars.xml");
// I don't think namespace URIs are really resolved. I'm not sure though -
// for a proof of concept, I suggest you use a namespace of
// http://dummy.com/dummy.xsd
XNamespace ns = "/carSchema.xsd";
var carInfo1 = from car in xmlDoc.Descendants(ns + "car")
select (string)car.Element(ns + "brand") + ": " +
(string)car.Element(ns + "model");