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

    运行结果:

  • 相关阅读:
    HDU 5302(Connect the Graph- 构造)
    Redis 集群
    HDFS集中式缓存管理(Centralized Cache Management)
    JavaScript语言基础12
    【IOS】启动画面
    小贝_mysql优化学习
    hdu2099 整除的位数(暴力)
    Receiver type ‘X’ for instance message is a forward declaration
    动态游标(比如表名作为參数)以及动态SQL分析
    mongodb与SQL相应关系表
  • 原文地址:https://www.cnblogs.com/ywkpl/p/2041458.html
Copyright © 2011-2022 走看看