2014-03-08 4 views
2

Хотите отобразить извлеченные данные из xml-файла в сетке свойств. XML файл выглядит примерно так:Отображение данных по сетке свойств

<?xml version="1.0" encoding="utf-8" ?> 
<customer> 
    <cust id="1"> 
     <id>120</id> 
     <name>hello</name> 

    </cust> 
    <cust id="2"> 

     <name>hello12</name> 
     <id>12012</id> 
    </cust> 
</customer> 

Теперь я хочу, чтобы извлечь данные из XML в таблице свойств с идентификатором сервера в категории (т.е. он должен отображать П идентификатор = «1» в одной категории и П ид = "2" во второй категории)

+1

Хорошо. что ты уже испробовал? В Интернете доступно множество примеров. См. Http://stackoverflow.com/help/how-to-ask для лучшего формирования вопроса. – Junaith

+0

Может быть n число узлов-клиентов. Я знаю, как показывать, когда категории уже известны (т. Е. Исправлено). – nihar

+0

ИСО-сеть может не соответствовать вашим требованиям. В основном вы хотите отобразить категориальный/иерархический список узлов. Для этого вы можете использовать элемент управления [TreeView] (http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview%28v=vs.110%29.aspx). – Junaith

ответ

1

Попробуйте этот образец кода:

[TypeConverter(typeof(CustomerObjectConverter))] 
public class Customer 
{ 
    internal readonly int ServerId; 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public Customer(int serverId) 
    { 
     ServerId = serverId; 
    } 

    public override string ToString() 
    { 
     return Id + ", " + Name; 
    } 
} 

public class CustomerCollection : CollectionBase, ICustomTypeDescriptor 
{ 
    public CustomerCollection() 
    { 
    } 

    public CustomerCollection(IEnumerable<Customer> collection) 
    { 
     foreach (var item in collection) 
      Add(item); 
    } 

    public void Add(Customer emp) 
    { 
     List.Add(emp); 
    } 
    public void Remove(Customer emp) 
    { 
     List.Remove(emp); 
    } 
    public Customer this[int index] 
    { 
     get { return (Customer)List[index]; } 
    } 

    public String GetClassName() 
    { 
     return TypeDescriptor.GetClassName(this, true); 
    } 

    public AttributeCollection GetAttributes() 
    { 
     return TypeDescriptor.GetAttributes(this, true); 
    } 

    public String GetComponentName() 
    { 
     return TypeDescriptor.GetComponentName(this, true); 
    } 

    public TypeConverter GetConverter() 
    { 
     return TypeDescriptor.GetConverter(this, true); 
    } 

    public EventDescriptor GetDefaultEvent() 
    { 
     return TypeDescriptor.GetDefaultEvent(this, true); 
    } 

    public PropertyDescriptor GetDefaultProperty() 
    { 
     return TypeDescriptor.GetDefaultProperty(this, true); 
    } 

    public object GetEditor(Type editorBaseType) 
    { 
     return TypeDescriptor.GetEditor(this, editorBaseType, true); 
    } 

    public EventDescriptorCollection GetEvents(Attribute[] attributes) 
    { 
     return TypeDescriptor.GetEvents(this, attributes, true); 
    } 

    public EventDescriptorCollection GetEvents() 
    { 
     return TypeDescriptor.GetEvents(this, true); 
    } 

    public object GetPropertyOwner(PropertyDescriptor pd) 
    { 
     return this; 
    } 
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
    { 
     return GetProperties(); 
    } 

    public PropertyDescriptorCollection GetProperties() 
    { 
     PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null); 

     for (int i = 0; i < List.Count; i++) 
     { 
      CustomersCollectionPropertyDescriptor pd = new CustomersCollectionPropertyDescriptor(this, i); 
      pds.Add(pd); 
     } 
     return pds; 
    } 
} 

internal class CustomersCollectionPropertyDescriptor : PropertyDescriptor 
{ 
    private readonly CustomerCollection collection; 
    private readonly int index = -1; 

    public CustomersCollectionPropertyDescriptor(CustomerCollection coll, int idx) 
     : base("#" + idx.ToString(), null) 
    { 
     collection = coll; 
     index = idx; 
    } 

    public override AttributeCollection Attributes 
    { 
     get { return new AttributeCollection(null); } 
    } 

    public override string Category 
    { 
     get { return "Server " + collection[index].ServerId; } 
    } 

    public override bool CanResetValue(object component) 
    { 
     return true; 
    } 

    public override Type ComponentType 
    { 
     get { return collection.GetType(); } 
    } 

    public override string DisplayName 
    { 
     get { return "Customer " + (index + 1); } 
    } 

    public override string Description 
    { 
     get { return "Customer"; } 
    } 

    public override object GetValue(object component) 
    { 
     return collection[index]; 
    } 

    public override bool IsReadOnly 
    { 
     get { return true; } 
    } 

    public override string Name 
    { 
     get { return "#" + index.ToString(); } 
    } 

    public override Type PropertyType 
    { 
     get { return collection[index].GetType(); } 
    } 

    public override void ResetValue(object component) {} 

    public override bool ShouldSerializeValue(object component) 
    { 
     return false; 
    } 

    public override void SetValue(object component, object value) 
    { 
    } 
} 

internal class CustomerObjectConverter : ExpandableObjectConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return false; 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
    { 
     return destinationType == typeof(string) && value is Customer 
        ? value.ToString() 
        : base.ConvertTo(context, culture, value, destinationType); 
    } 
} 

Применение. Вставьте следующий код в конструктор формы, содержащей PropertyGrid с именем PropertyGrid:

const string xmlString = "<customer><cust id=\"1\"><id>120</id><name>hello</name></cust><cust id=\"2\"><name>hello12</name><id>12012</id></cust></customer>"; 
XmlDocument xml = new XmlDocument(); 
xml.LoadXml(xmlString); 
XmlNode root = xml["customer"]; 

CustomerCollection customers = new CustomerCollection(); 

foreach (XmlNode node in root.ChildNodes) 
    customers.Add(new Customer(int.Parse(node.Attributes["id"].Value)) 
     { 
      Id = int.Parse(node["id"].InnerText), 
      Name = node["name"].InnerText 
     }); 

propertyGrid.SelectedObject = customers; 
+0

Спасибо за код. Он работает – nihar

+1

Рад помочь вам. Как насчет награды за мой ответ? – Dmitry

+0

Я не уверен, как наградить награду. Если вы можете помочь мне в награждении – nihar

Смежные вопросы