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

    运行结果:

  • 相关阅读:
    Codeforces 362D Fools and Foolproof Roads 构造题
    Machine Learning—Mixtures of Gaussians and the EM algorithm
    Php面向对象 – 单例模式
    标C编程笔记day06 动态分配内存、函数指针、可变长度參数
    html的下拉框的几个基本使用方法
    【创新培育项目】为什么要组队參加比赛?及如何寻找一个合适的选题?
    项目管理心得:一个项目经理的个人体会、经验总结
    GMM的EM算法实现
    oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
    sql server 2005 32位+64位、企业版+标准版、CD+DVD 下载地址大全
  • 原文地址:https://www.cnblogs.com/ywkpl/p/2041458.html
Copyright © 2011-2022 走看看