0

我正在从 XML 中读取数据,如下所示:(Contacts.xml)

    <?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 到列?

IE:774-6655-252 - mobile

谢谢。

当我运行它时,它向我显示一个错误:

无法确定 MetaTable。无法为数据源“ObjectDataSource1”确定 MetaTable,并且无法从请求 URL 中推断出 MetaTable。确保表映射到数据源,或者数据源配置了有效的上下文类型和表名,或者请求是注册的 DynamicDataRoute 的一部分。

4

1 回答 1

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();

希望你能明白。

于 2012-04-08T00:03:36.817 回答