-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'不是*擴展方法。只是一個靜態方法。 –