2015-01-08 8 views
-3

왜 내 확장 방법이 입니다. 데이터가있는 XElements를 전달하면 GetVendorForXMLElement()가 NRE에서 분출됩니까?null이 아닌 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; } 
} 

데이터입니다; 내가 사용 "(GetVendorForXMLElement) 선택"에 상관없이 통화의 세 가지 유형 중 어떤의

enter image description here

:이 I)가 MessageBox.Show (과 참조 XDocument.Parse() 호출하기 전에 부르는 , NRE가 발생합니다. CompanyName 요소의 꺾쇠 괄호 ("[blank]")입니까? 또는...???

+1

LINQ 쿼리를 부분으로 나누고 각각을 개별적으로 디버깅 해 봤니? –

+1

스택 추적을 표시 할 수 있습니까? 또한,'GetVendorForXMLElement'은 확장 메소드가 아닙니다. 그냥 정적 메서드. –

답변

3

귀하의 요소는 VendorId 요소가 아닌 VendorID 요소 (케이스를 알), Element("VendorID") 따라서 null를 반환하고, 그에 Value를 호출하는 것은 NRE를 throw있다.