请教各位大侠,我现在有一个问题,我在做一个WPF的项目,其中有一个地方有2个ListBox,第一个ListBox列出几个类的类名,选择其中一个,则在第二个ListBox中列出这个类的所有属性。已通过反射完成。
但是现在的问题是,这些类的有些属性是不需要的,因此就需要进行过滤。我想硬编码应该不是一个好的选择,而我了解到好像可以通过使用一个XML文件来把这些属性作为节点。然后从这个XML文件中读取结点。无奈小弟对XML一直不太上心,所以不知道怎么做,希望能得到大家的指点。
我的想法有两种:
1.把需要的属性写到XML文件中,然后在代码中读取这些结点.
2.把所有的属性写到XML文件中,通过在XML文件中做一些标记,然后代码中通过这些标记来判断是否取这个结点.
这两种方法分别如何实现?谢谢!!!!!!!!!!!跪谢!!!!!!!!!!!!!!

解决方案 »

  1.   

    选择XML 是正确的,推荐:
    2.把所有的属性写到XML文件中,通过在XML文件中做一些标记,然后代码中通过这些标记来判断是否取这个结点.
      

  2.   


    问题是我不会啊...哈哈,XML文件中的判断结点应该怎么写?C#代码读XML结点的时候应该怎么判断?能给个简单的例子不?谢谢
      

  3.   

    我博客里面的LZ看看 不过是c#的  传送门
      

  4.   

    linq to xml选择节点很简单的XElement xroot = XElement.Load(fileName);
                    var xmllist = xroot.Elements("SafetyManager").Where(p => p.Attribute("Date").Value == dtNow.ToString("yyyy-MM-dd"));
                    if (xmllist.Count() > 0)
                    {
                        XElement xtemp = xmllist.First();
                        xtemp.SetAttributeValue("Fraction", (AllFr / AllScore).ToDecimal().ToFix(2));
                    }
                    else
                    {
                        XElement NewNode = new XElement("SafetyManager",
                                     new XAttribute("Date", dtNow.ToString("yyyy-MM-dd")),
                                     new XAttribute("Fraction", (AllFr / AllScore).ToDecimal().ToFix(2)));
                        xroot.Add(NewNode);
                    }                xroot.Save(fileName);
    这是随便写的一个
      

  5.   

    用XmlDocument,给你一段写属性的例子:
    using System;
    using System.IO;
    using System.Xml;public class Sample
    {
      public static void Main()
      {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "</book>");    //Create an attribute.
        XmlAttribute attr = doc.CreateAttribute("publisher");
        attr.Value = "WorldWide Publishing";
              
        //Add the new node to the document. 
        doc.DocumentElement.SetAttributeNode(attr);
            
        Console.WriteLine("Display the modified XML...");        
        doc.Save(Console.Out);
      }
    }