zoukankan      html  css  js  c++  java
  • XML杂记

    1、需注意XNameSpace
    用XDocument修改一XML,載入後用Root.Element(“節點名稱")方法去讀取,結果總是返回null

    後發現根源是Root上 xmlns=""屬性,此屬性是聲明了XML的NameSpace

    這時獲取Element的方式應改為:

    XDocument xmlDoc = XDocument.Load("c:\fileName.xml");
    XNamespace xmlNamespace 
    = this.xmlDoc.Root.Name.Namespace;
    XElement xelement 
    = xmlDoc.Element(xmlNamespace + "NodeName");

    2、查找子节点方法  XDocument.Descendants();

      XDocument.Elements()使用后不可以使用Element进行子节点的查询或者过滤,很不方便,怎么解决,XDocument.Descendants()方法:

    //Data
    static XDocument GetStarbuzzData()
    {
         XDocument doc = new XDocument(
             new XDeclaration("1.0", "utf-8", "yes"),
             new XComment("Starbuzz Customer Loyalty Data"),
             new XElement("starbuzzData",
                 new XAttribute("storeName", "Park Slope"),
                 new XAttribute("location", "Brooklyn, NY"),
                 new XElement("person",
                     new XElement("personalInfo",
                         new XElement("name", "Janet Venutian"),
                         new XElement("zip", 11215)),
                     new XElement("favoriteDrink", "Choco Macchiato"),
                     new XElement("moneySpent", 255),
                     new XElement("visits", 50)),
                 new XElement("person",
                     new XElement("personalInfo",
                         new XElement("name", "Liz Nelson"),
                         new XElement("zip", 11238)),
                     new XElement("favoriteDrink", "Double Cappuccino"),
                     new XElement("moneySpent", 150),
                     new XElement("visits", 35)),
                 new XElement("person",
                     new XElement("personalInfo",
                         new XElement("name", "Matt Franks"),
                         new XElement("zip", 11217)),
                     new XElement("favoriteDrink", "Zesty Lemon Chai"),
                     new XElement("moneySpent", 75),
                     new XElement("visits", 15)),
                 new XElement("person",
                     new XElement("personalInfo",
                         new XElement("name", "Joe Ng"),
                         new XElement("zip", 11217)),
                     new XElement("favoriteDrink", "Banana Split in a Cup"),
                     new XElement("moneySpent", 60),
                     new XElement("visits", 10)),
                 new XElement("person",
                     new XElement("personalInfo",
                         new XElement("name", "Sarah Kalter"),
                         new XElement("zip", 11215)),
                     new XElement("favoriteDrink", "Boring Coffee"),
                     new XElement("moneySpent", 110),
                     new XElement("visits", 15))));
         return doc;
    }
    
    //查询
    static void QueryTheData(XDocument doc)
    {
         var data = from item in doc.Descendants("person")
                     select new
                     {
                          drink = item.Element("favoriteDrink").Value,
                          moneySpent = item.Element("moneySpent").Value,
                          zipCode = item.Element("personalInfo").Element("zip").Value
                      };
         foreach (var p in data)
             Console.WriteLine(p.ToString());
     
         var zipcodeGroups = from item in doc.Descendants("person")
                             group item.Element("favoriteDrink").Value
                             by item.Element("personalInfo").Element("zip").Value
                                 into zipcodeGroup
                                 select zipcodeGroup;
         foreach (var group in zipcodeGroups)
             Console.WriteLine("{0} favorite drinks in {1}",
                             group.Distinct().Count(), group.Key);
    }

    运行结果:

  • 相关阅读:
    centos6下安装配置DNS(bind)
    centos6下安装配置NFS
    如何在kettle里面加载hadoop
    jQuery练习JavaScript事件与事件对象
    摆上殷勤的工作&职场用祷文
    Ajax自动完成(autocomplete)响应文本框输入后显示模糊数据列表
    未来的书香
    重要的计算机学习资料(累计中)
    W4F学习笔记之一
    软件测试重要资料汇集
  • 原文地址:https://www.cnblogs.com/ywkpl/p/2041458.html
Copyright © 2011-2022 走看看