2012-04-07 74 views
0

我從XML讀取數據是這樣的: (Contacts.xml)的LINQ to XML和結合的GridView

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
    <!--LINQ to XML Contacts XML Example--> 
    <?MyApp 123-44-4444?> 
    <contacts> 
     <contact> 
     <name>Patrick Hines</name> 
     <phone type="home">206-555-0144</phone> 
     <phone type="work">425-555-0145</phone> 
     <address> 
      <street1>123 Main St</street1> 
      <city>Mercer Island</city> 
      <state>WA</state> 
      <postal>68042</postal> 
     </address> 
     </contact> 
     <contact> 
     <name>Gretchen Rivas</name> 
     <phone type="mobile">206-555-0163</phone> 
     <address> 
      <street1>123 Main St</street1> 
      <city>Mercer Island</city> 
      <state>WA</state> 
      <postal>68042</postal> 
     </address> 
     </contact> 
    </contacts> 

我從XML文件中獲取數據的代碼: (contacts.cs)

public class Contact 
    { 
//static members 
public static string fLocation; 

//private members 
private string name; 
private List<PhoneNumber> pNumber; 
private Adress cAdress; 

//public members 
public Adress CAdress 
{ 
    get { return cAdress; } 
} 
public List<PhoneNumber> PNumber 
{ 
    get { return pNumber; } 
} 
public string Name 
{ 
    get { return name; } 
} 

//Constructor 
public Contact(string _name, List<PhoneNumber> _pNumber,Adress _cAdress) 
{ 
    name = _name; 
    pNumber = _pNumber; 
    cAdress = _cAdress; 
} 

public static List<Contact> Get() 
{ 
    List<Contact> output = new List<Contact>(); 

    XDocument data = XDocument.Load(fLocation); 

    var query = from c in data.Descendants("contact") 
       orderby c.Element("name").Value 
       select c; 

    foreach (var item in query) 
    { 
     List<PhoneNumber> pNumber = new List<PhoneNumber>(); 

     foreach (var PhoneNumbers in item.Elements("phone")) 
     { 
      pNumber.Add(new PhoneNumber(PhoneNumbers.Value,PhoneNumbers.Attribute("type").Value)); 
     } 

     Adress cAdress = new Adress(item.Element("address").Element("street1").Value, 
      item.Element("address").Element("city").Value, 
      item.Element("address").Element("state").Value, 
      item.Element("address").Element("postal").Value); 

     output.Add(new Contact(item.Element("name").Value,pNumber,cAdress)); 
    } 

    return output; 
} 

//subclasses 
public class Adress 
{ 
    private string street; 
    private string city; 
    private string state; 
    private string postal; 

    public string Postal 
    { 
     get { return postal; } 
    }   
    public string State 
    { 
     get { return state; } 
    } 
    public string City 
    { 
     get { return city; } 
    } 
    public string Street 
    { 
     get { return street; } 
    } 


    public Adress(string _street,string _city, string _state, string _postal) 
    { 
     street = _street; 
     city = _city; 
     state = _state; 
     postal = _postal; 
    } 
} 

public class PhoneNumber 
{ 
    private string number; 
    private string numberType; 


    public string NumberType 
    { 
     get { return numberType; } 
    } 
    public string Number 
    { 
     get { return number; } 
    } 


    public PhoneNumber(string _number, string _phoneNumberType) 
    { 
     number = _number; 
     numberType = _phoneNumberType; 
    } 
} 

}

而且我想以某種方式結合的GridView: (Default.aspx的)

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataSourceID="ObjectDataSource1"> 
     <Columns> 
      <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" 
       SortExpression="Name" /> 
      <asp:DynamicField DataField="CAdress" HeaderText="CAdress" /> 
      <asp:DynamicField DataField="PNumber" HeaderText="PNumber" /> 
     </Columns> 
    </asp:GridView> 
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get" 
     TypeName="LINQtoXML_WebForms.Contact"></asp:ObjectDataSource> 

任何想法如何綁定例如Contact.PhoneNumber.number +「 - 」Contact.PhoneNumber.numberType colomn?

即:774-6655-252 - mobile

感謝。

當我運行它,它讓我看到了一個錯誤:

Could not determine a MetaTable. A MetaTable could not be determined for the data source 'ObjectDataSource1' and one could not be inferred from the request URL. Make sure that the table is mapped to the dats source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute.

回答

2

我認爲你要麼必須的元信息添加到您的類或使用方法,我會用 - 自己寫的方法,而不是使用objectdatasource對象。

例如LINQ到XML

public List<Contact> GetContacts() 
{ 
    XDocument doc = XDocument.Load("path_to_some_file.xml"); 

    var contacts = from o in doc.Descendants("contact") 
        select new Contact() 
        { 
         Name = (string)o.Element("Name"), 
         Phone = (string)o.Element("Phone") 
        }; 

    return contacts.ToList(); 
} 

,然後只需將它綁定在代碼隱藏您的控制:

GridView1.DataSource = GetContacts(); 
GridView1.DataBind(); 

希望你的想法。