我无法从我的 XML 中获取元素。我无法从我的 XML 中获取 Vehicles 或 Vehicle 元素,它总是返回 null。
任何人都可以看到我哪里出错了吗?
这是我的代码...
[TestMethod]
public void TestDeleteVehicleFromXMLFile()
{
using (FileStream stream = new FileStream(base._TestXPathXMLFile, FileMode.Open))
{
try
{
XDocument xDoc = XDocument.Load(stream);
var q = from RootNode in xDoc.Descendants("VehicleCache")
select new
{
// Vehicles & VehiclesList is always null
Vehicles = RootNode.Elements(XName.Get("Vehicle")).ToList(),
VehiclesList = RootNode.Elements(XName.Get("Vehicles")).ToList(),
SelfNode = RootNode.DescendantNodesAndSelf().ToList(),
DescendantNodes = RootNode.DescendantNodes().ToList()
};
// used to see what is in item
foreach (var item in q)
{
int i = 0;
}
}
catch(Exception E)
{
Assert.Fail(E.Message);
}
}
}
<VehicleCache>
<Vehicles xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.myURL.co.uk">
<Vehicle>
<CapCode>41653</CapCode>
<TrueCap i:nil="true" />
<VehicleID>365789</VehicleID>
<ViperVehicleID i:nil="true" />
<BodyTypeID>5</BodyTypeID>
</Vehicle>
<Vehicle>
<CapCode>42565</CapCode>
<TrueCap i:nil="true" />
<VehicleID>365845</VehicleID>
<ViperVehicleID i:nil="true" />
<BodyTypeID>2</BodyTypeID>
</Vehicle>
</Vehicles>