zoukankan      html  css  js  c++  java
  • Linq and XML

    https://msdn.microsoft.com/en-us/library/bb943906.aspx

    Basic Queries (LINQ to XML)

    1. add to xml document

     public void sendMsg(string senderID, string receiverID, string msg)
            {
                String path = HostingEnvironment.MapPath(@"/App_Data/msgdb.xml");
                XElement root = XElement.Load(path);
                root.Add(
                    new XElement("msg",
                        new XElement("sendid", senderID),
                        new XElement("rcvid", receiverID),
                        new XElement("content", msg)
                ));
                root.Save(path);
            }

    2. query xml using linq

     public string[] receiveMsg(string receiverID)
            {
                String path = HostingEnvironment.MapPath(@"/App_Data/msgdb.xml");
                XElement root = XElement.Load(path);
                IEnumerable<String> query =
                    from i in root.Elements("msg")
                    where ((string)i.Element("rcvid")).Trim() == receiverID
                    select (string)i.Element("content");
                string[] result = new string[query.Count()];
                for (int i = 0; i < query.Count(); i++)
                {
                    result[i] = query.ElementAt(i);
                }
                return result;
            }

    xml used in example

    <?xml version="1.0" encoding="utf-8"?>
    <msgs>
      <msg>
        <sendid>
          123
        </sendid>
        <rcvid>
          321
        </rcvid>
        <content>
          hello
        </content>
      </msg>
      <msg>
        <sendid>555</sendid>
        <rcvid>666</rcvid>
        <content>lily i love you</content>
      </msg>
    </msgs>
  • 相关阅读:
    解决Servlet无法换行
    Servlet页面解析中文乱码问题
    IDEA2020版创建Servlet(Web项目)完整教程
    SPFA算法
    最短路算法Dijkstra
    搜索与图论总结
    Kruskal算法
    第10章 嵌入式Linux 的调试技术
    第9章 硬件抽象层:HAL
    第八章 让开发板发出声音:蜂鸣器驱动
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/4422304.html
Copyright © 2011-2022 走看看