2012-12-29 4 views
0

특성과 하위 항목이 모두있는 노드를 구문 분석하는 데 어려움을 겪고 있습니다.노드 아래에 여러 속성 값 가져 오기

고객 노드에 들어가서 그 아래의 모든 것을 웹 페이지에서 사용할 값으로 파싱해야합니다.

var contacts = from c in xdoc.Descendants("contact") 
       select new Contact 
       {    
        Email = (string)c.Element("email"), 
        HomePhone = (string)c.Element("phone").Attribute("type") // ? 
       }; 

그리고 내 XML :

<customer> 
      <contact> 
      <name part="first">Rick</name> 
      <name part="last">Smith</name> 
      <email>[email protected]</email> 
      <phone type="home">2299998989</phone> 
      <phone type="work">2298887878</phone> 
      <phone type="cell">2297778878</phone> 
      <address type="home"> 
       <street line="1">4001 Coleman Rd</street> 
       <street line="2">Ste. 99</street> 
       <city>Tempe</city> 
       <regioncode>AZ</regioncode> 
       <postalcode>43444</postalcode> 
      </address> 
      </contact> 
      <comments>Customer comments</comments> 
     </customer> 

     <vendor> 
      <contact> 
      <name part="full" type="salesperson">Joe Smith</name> 
      </contact> 
     </vendor> 
+0

어떤 오류가 발생 했습니까? 귀하의 코드는 괜찮아 보인다. 단지 불완전하다. – Tilak

답변

2
XDocument xdoc = XDocument.Load(path_to_xml); 
var customers = 
    from c in xdoc.Descendants("customer") 
    let contact = c.Element("contact")   
    let names = contact.Elements("name") 
    let phones = contact.Elements("phone") 
    let address = contact.Element("address") 
    let streets = address.Elements("street") 
    select new Customer { 
     Contact = new Contact { 
      FirstName = (string)names.SingleOrDefault(n => (string)n.Attribute("part") == "first"), 
      LastName = (string)names.SingleOrDefault(n => (string)n.Attribute("part") == "last"), 
      Email = (string)contact.Element("email"), 
      HomePhone = (string)phones.SingleOrDefault(p => (string)p.Attribute("type") == "home"), 
      CellPhone = (string)phones.SingleOrDefault(p => (string)p.Attribute("type") == "cell"), 
      WorkPhone = (string)phones.SingleOrDefault(p => (string)p.Attribute("type") == "work"), 
      Address = new Address { 
        Type = (string)address.Attribute("type"), 
        StreetLine1 = (string)streets.SingleOrDefault(s => (int)s.Attribute("line") == 1), 
        StreetLine2 = (string)streets.SingleOrDefault(s => (int)s.Attribute("line") == 2), 
        City = (string)address.Element("city"), 
        RegionCode = (string)address.Element("regioncode"), 
        PostalCode = (string)address.Element("postalcode") 
       } 
     }, 
     Comments = (string)c.Element("comments") 
    }; 

당신이 필요로하는 클래스를 다음과 같은 구문 분석 된 데이터 보유 :

public class Customer 
{ 
    public Contact Contact { get; set; } 
    public string Comments { get; set; } 
} 

public class Contact 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Email { get; set; } 
    public string HomePhone { get; set; } 
    public string WorkPhone { get; set; } 
    public string CellPhone { get; set; } 
    public Address Address { get; set; } 
} 

public class Address 
{ 
    public string Type { get; set; } 
    public string StreetLine1 { get; set; } 
    public string StreetLine2 { get; set; } 
    public string City { get; set; } 
    public string RegionCode { get; set; } 
    public string PostalCode { get; set; } 
} 
+0

젠체하는 ..이 바위들과 당신은 그것에 빠르다 !! 나는이 프로젝트를 완료하는 데 약 한 시간의 노력을 기울이고 있습니다 ... 지불 할 것이고, 자세한 내용은 [email protected]으로 이메일을 보내주십시오. – user1934500

1

집 전화 번호를 얻으려면 당신은을 사용한다 :

HomePhone = (string)c.Elements("phone").SingleOrDefault(e => (string)e.Attribute("type") == "home");