LINQ to XML基础

来源:互联网 发布:骨肉之躯 知乎 编辑:程序博客网 时间:2024/06/08 04:32

employee.cs实体类

 public class employee    {        public string lastName { get; set; }        public string firstName { get; set; }        public string city { get; set; }        public string state { get; set; }    }

Program.cs

  class Program    {        static void Main(string[] args)        {            List<employee> employees = new List<employee>()            {                new employee() {firstName="John",lastName="Smith",city="哈佛镇",state="PA"},                new employee() {firstName="Jane",lastName="Doe"  ,city="尤因",state="NJ"},                new employee() {firstName="Jack",lastName="Jones",city="华盛顿堡",state="PA"},            };            //从对象列表创建XML结构            var XMLEmployees = new XElement("Query", from e in employees                                                     select new XElement("Employee",                                                                 new XElement("FirstName", e.firstName),                                                                 new XElement("LastName", e.lastName),                                                                 new XElement("City", e.city),                                                                 new XElement("State", e.state)                                                                 )                                            );            Console.WriteLine(XMLEmployees);            Console.ReadKey();        }    }

运行结果如图:

这里写图片描述


LINQ解析XML文件

Program.cs类:

   class Initialization    {        //加载一个xml文件        XDocument doc = XDocument.Load("services.xml");        public void logic()        {            //从<item> </ item>中选择来获取我们需要的元素(<type> </ type>)Linq            var services = from element in doc.Descendants("item")                           select new                           {                               // 在这种情况下,我们寻找<type> </ type>                               Item = element.Element("type").Value,                           };            //输出                   foreach (var element in services)            {                Console.WriteLine(element.Item);            }            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////            Console.WriteLine();            Console.WriteLine("从上面的列表中输入。要退出,键入exit");            // 它运行直到用户键入exit            do            {                string inputservice2 = Console.ReadLine();                if (!"exit".Equals(inputservice2))                {                    // 从<item> </ item>和<type> </ type>中选择get(<wsdl> </ wsdl>)wsdl                    var selectedService2 = from r in doc.Descendants("item").Where(r => inputservice2 == r.Element("type").Value)                                           select new                                           {                                               wsdl = r.Element("wsdl").Value,                                           };                    // 错误信息                    if (!selectedService2.Any().Equals(false))                    {                        foreach (var r in selectedService2)                        {                            Console.WriteLine(r.wsdl);                        }                    }                    else                    {                        Console.WriteLine("你已经从上面的列表中输入不正确的服务类型");                    }                }                else                {                    break;                }            }            while (true);        }    }    class Program    {        static void Main(string[] args)        {            Initialization init = new Initialization();            init.logic();        }    }

services.xml

<?xml version="1.0" encoding="utf-8"?><endpoints>  <item>    <type>oper</type>    <name>工人(操作者)1</name>    <address>http://service.com:8007/oper1/MBroker.svc</address>    <wsdl>http://service.com:8007/oper1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>oper</type>    <name>工人(操作者)2</name>    <address>http://service.com:8007/oper2/MBroker.svc</address>    <wsdl>http://service.com:8007/oper2/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>oper</type>    <name>工人(操作者)3</name>    <address>http://service.com:8009/oper3/MBroker.svc</address>    <wsdl>http://service.com:8009/oper3/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>oper</type>    <name>工人(操作者)4</name>    <address>http://service.com:8009/oper4/MBroker.svc</address>    <wsdl>http://service.com:8009/oper4/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>work</type>    <name>工作1</name>    <address>http://service.com:8059/work1/MBroker.svc</address>    <wsdl>http://service.com:8059/work1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>work</type>    <name>工作2</name>    <address>http://service.com:8059/work2/MBroker.svc</address>    <wsdl>http://service.com:8059/work2/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>work</type>    <name>工作3</name>    <address>http://service.com:8059/work3/MBroker.svc</address>    <wsdl>http://service.com:8059/work3/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>work</type>    <name>工作4</name>    <address>http://service.com:8059/work4/MBroker.svc</address>    <wsdl>http://service.com:8059/work4/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>work</type>    <name>工作5</name>    <address>http://192.168.0.2:8985/MBroker.svc</address>    <wsdl>http://192.168.0.2:8985/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>false</enabled>  </item>  <item>    <type>work</type>    <name>工作6</name>    <address>http://192.168.0.2:1234/MBroker.svc</address>    <wsdl>http://192.168.0.2:1234/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>false</enabled>  </item>  <item>    <type>work</type>    <name>工人(河畔切尔内)</name>    <address>http://192.168.0.3/MBroker_webarm_test/MBroker.svc</address>    <wsdl>http://192.168.0.3/MBroker_webarm_test/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>false</enabled>  </item>  <item>    <type>edu</type>    <name>教育</name>    <address>http://service.com:8156/edu1/MBroker.svc</address>    <wsdl>http://service.com:8156/edu1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>false</enabled>  </item>  <item>    <type>edu</type>    <name>教育 2</name>    <address>http://service.com:8156/edu2/MBroker.svc</address>    <wsdl>http://service.com:8156/edu2/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>test</type>    <name>测试</name>    <address>http://service.com:8217/test1/MBroker.svc</address>    <wsdl>http://service.com:8217/test1/MBroker.svc?wsdl</wsdl>    <primary>true</primary>    <enabled>true</enabled>  </item>  <item>    <type>dev</type>    <name>设计 (1)</name>    <address>http://dev.service.com:8251/dev1/MBroker.svc</address>    <wsdl>http://dev.service.com:8251/dev1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>dev</type>    <name>发展(卡马河畔切尔尼)</name>    <address>http://192.168.0.1:80/MBroker/MBroker.svc</address>    <wsdl>http://192.168.0.1:80/MBroker/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>false</enabled>  </item>  <item>    <type>mbrokertestpool</type>    <name>试验池</name>    <address>http://192.168.0.4/MBroker/MBroker.svc</address>    <wsdl>http://192.168.0.4/MBroker/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>beta</type>    <name>Бета</name>    <address>http://service.com:4321/MBroker.svc</address>    <wsdl>http://service.com:4321/MBroker.svc?wsdl</wsdl>    <primary>true</primary>    <enabled>true</enabled>  </item>  <item>    <type>service</type>    <name>Сервисный 1</name>    <address>http://service.com:1111/services1/MBroker.svc</address>    <wsdl>http://service.com:1111/services1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>servicedev</type>    <name>服务(发展)</name>    <address>http://service.com:1111/services_dev1/MBroker.svc</address>    <wsdl>http://service.com:1111/services_dev1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>servicetest</type>    <name>服务(测试)</name>    <address>http://service.com:1111/services_test1/MBroker.svc</address>    <wsdl>http://service.com:1111/services_test1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>  <item>    <type>servicepay</type>    <name>服务(支付)</name>    <address>http://service.com:1111/services_pay1/MBroker.svc</address>    <wsdl>http://service.com:1111/services_pay1/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item>   <item>    <type>fbroker</type>    <name>文件经纪人</name>    <address>http://service.com:6549/fbroker1/MBroker/MBroker.svc</address>    <wsdl>http://service.com:6549/fbroker1/MBroker/MBroker.svc?wsdl</wsdl>    <primary>true</primary>    <enabled>true</enabled>  </item>  <item>    <type>fbrokerdev</type>    <name>文件经纪人(发展)</name>    <address>http://service.com:5555/MBroker/MBroker.svc</address>    <wsdl>http://service.com:5555/MBroker/MBroker.svc?wsdl</wsdl>    <primary>true</primary>    <enabled>true</enabled>  </item>    <item>    <type>mbrokertestpool</type>    <name>试验池</name>    <address>http://192.168.0.5/MBroker/MBroker.svc</address>    <wsdl>http://192.168.0.5/MBroker/MBroker.svc?wsdl</wsdl>    <primary>false</primary>    <enabled>true</enabled>  </item></endpoints>

运行结果如图:

这里写图片描述


Orderquery.cs实体类:

public class Orderquery    {        public string return_code { get; set; }        public string return_msg { get; set; }        public string appid { get; set; }        public string mch_id { get; set; }        public string device_info { get; set; }        public string nonce_str { get; set; }        public string sign { get; set; }        public string result_code { get; set; }        public string openid { get; set; }        public string is_subscribe { get; set; }        public string trade_type { get; set; }        public string bank_type { get; set; }        public string total_fee { get; set; }        public string fee_type { get; set; }        public string transaction_id { get; set; }        public string out_trade_no { get; set; }        public string attach { get; set; }        public string time_end { get; set; }        public string trade_state { get; set; }    }

Orderquery.xml

<xml>  <return_code><![CDATA[SUCCESS]]></return_code>  <return_msg><![CDATA[OK]]></return_msg>  <appid><![CDATA[wx2421b1c4370ec43b]]></appid>  <mch_id><![CDATA[10000100]]></mch_id>  <device_info><![CDATA[1000]]></device_info>  <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>  <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>  <result_code><![CDATA[SUCCESS]]></result_code>  <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>  <is_subscribe><![CDATA[Y]]></is_subscribe>  <trade_type><![CDATA[MICROPAY]]></trade_type>  <bank_type><![CDATA[CCB_DEBIT]]></bank_type>  <total_fee>1</total_fee>  <fee_type><![CDATA[CNY]]></fee_type>  <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>  <out_trade_no><![CDATA[1415757673]]></out_trade_no>  <attach><![CDATA[订单额外描述]]></attach>  <time_end><![CDATA[20141111170043]]></time_end>  <trade_state><![CDATA[SUCCESS]]></trade_state></xml>

Program.cs

using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Xml;using System.Xml.Linq;using XmlTest.Model;namespace XmlTest{    class Program    {        static void Main(string[] args)        {            XDocument xmlDocument = XDocument.Load(Environment.CurrentDirectory + "/unifiedorder.xml");            XElement xmlContact = xmlDocument.Element("xml");            Unifiedorder order = new Unifiedorder();            order.appid = xmlContact.Element("appid").Value;            order.attach = xmlContact.Element("attach").Value;            order.body = xmlContact.Element("body").Value;            order.detail = xmlContact.Element("detail").Value;            order.mch_id = xmlContact.Element("mch_id").Value;            order.nonce_str = xmlContact.Element("nonce_str").Value;            order.notify_url = xmlContact.Element("notify_url").Value;            order.openid = xmlContact.Element("openid").Value;            order.out_trade_no = xmlContact.Element("out_trade_no").Value;            order.sign = xmlContact.Element("sign").Value;            Console.WriteLine(getProperties(order));            Console.WriteLine("----------------------------------------------------");            string content = xmlDocument.ToString();            /* Xml转换到实体对象 */            XDocument doc = XDocument.Parse(content);            Unifiedorder order2 = new Unifiedorder();            order2.appid = xmlContact.Element("appid").Value;            order2.attach = xmlContact.Element("attach").Value;            order2.body = xmlContact.Element("body").Value;            order2.detail = xmlContact.Element("detail").Value;            order2.mch_id = xmlContact.Element("mch_id").Value;            order2.nonce_str = xmlContact.Element("nonce_str").Value;            order2.notify_url = xmlContact.Element("notify_url").Value;            order2.openid = xmlContact.Element("openid").Value;            order2.out_trade_no = xmlContact.Element("out_trade_no").Value;            order2.sign = xmlContact.Element("sign").Value;            Console.WriteLine(getProperties(order2));            Console.ReadKey();        }        public static string getProperties<T>(T t)        {            string tStr = string.Empty;            if (t == null)            {                return tStr;            }            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);            if (properties.Length <= 0)            {                return tStr;            }            foreach (PropertyInfo item in properties)            {                string name = item.Name;                object value = item.GetValue(t, null);                if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))                {                    tStr += string.Format("{0}:{1}\r\n", name, value);                }                else                {                    getProperties(value);                }            }            return tStr;        }    }}

运行结果如图:

这里写图片描述

原创粉丝点击