-3
为什么我的扩展方法GetVendorForXMLElement()在我将它传递给XElements数据时在NRE中爆发?为什么非空XElements会导致NRE?
相同的代码(除了使用的自定义类,这里是“供应商”)与其他类/数据一起工作,但不在这里。我在GetVendorForXMLElement()方法中得到一个NRE。
private void buttonVendors_Click(object sender, EventArgs e)
{
IEnumerable<Vendor> vendors = GetCollectionOfVendors();
}
private IEnumerable<Vendor> GetCollectionOfVendors()
{
ArrayList arrList = FetchDataFromServer("http://localhost:21608/api/vendor/getall/dbill/ppus/42");
String contents = "<Vendors>";
foreach (String s in arrList)
{
contents += s;
}
contents += "</Vendors>";
String unwantedPreamble = "<ArrayOfVendor xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS\">";
contents = contents.Replace(unwantedPreamble, String.Empty);
contents = contents.Replace("</ArrayOfVendor>", String.Empty);
MessageBox.Show(contents);
XDocument xmlDoc = XDocument.Parse(contents);
// The result (NRE) is the same with any one of these three "styles" of calling Select()
//IEnumerable<Vendor> vendors = xmlDoc.Descendants("Vendor").Select(GetVendorForXMLElement).ToList();
//IEnumerable<Vendor> vendors = xmlDoc.Descendants("Vendor").Select(x => GetVendorForXMLElement(x));
IEnumerable<Vendor> vendors = xmlDoc.Descendants("Vendor").Select<XElement, Vendor>(GetVendorForXMLElement);
return vendors;
}
private static Vendor GetVendorForXMLElement(XElement vendor)
{
return new Vendor
{
CompanyName = vendor.Element("CompanyName").Value,
VendorID = vendor.Element("VendorID").Value
};
}
public class Vendor
{
public String CompanyName { get; set; }
public String VendorID { get; set; }
public String siteNum { get; set; }
}
有是数据;这是我看到与MessageBox.Show()之前XDocument.Parse()调用拨打:
即使是在使用三种类型的呼叫的以“选择(GetVendorForXMLElement)”我用,NRE发生。它是CompanyName元素中的尖括号(“[blank]”)吗?要么...???
您是否尝试将您的LINQ查询拆分为零件并分别调试每个零件? –
你可以显示堆栈跟踪吗?另外,'GetVendorForXMLElement'不是*扩展方法。只是一个静态方法。 –