Linq to XML Linq读取MXL(XDocument.Load()、XDocument.Parse()、XElement.Load()、XElement.Parse())

前端之家收集整理的这篇文章主要介绍了Linq to XML Linq读取MXL(XDocument.Load()、XDocument.Parse()、XElement.Load()、XElement.Parse())前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

命名空间:

System.Xml.Linq

一、Linq读取XML

1、demo.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <note>
  3. <conf>
  4. <to>infozero</to>
  5. <from>lerroy</from>
  6. <heading>测试信息</heading>
  7. <body>第一条测试信息</body>
  8. <title name="我的第一条消息">from myself</title>
  9. </conf>
  10. <conf>
  11. <to>infozero@163.com</to>
  12. <from>text</from>
  13. <heading> 时刻提醒我 </heading>
  14. <body>这是一条测试信息!</body>
  15. <title name="我的第二条消息">from others</title>
  16. </conf>
  17. </note>

2、读取XML

  1. XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("demo.xml"));
  2. var text = from t in doc.Descendants("conf")
  3. .Where(w => w.Element("to").Value.Contains('@'))
  4. select new
  5. {
  6. to = t.Element("to").Value,froms = t.Element("from").Value,body = t.Element("body").Value
  7. };
  8.  
  9. foreach (var a in text)
  10. {
  11. Response.Write(a.to+a.froms+a.body);
  12. }

二、加载和解析XML内容

  1. string myElement = @"<Car id='3'>
  2. <Color>Yellow</Color>
  3. <Make>Yugo</Make>
  4. </Car>";
  5.  
  6. XElement newElemment = XElement.Parse(myElement);
  7. newElemment.Save(System.Web.HttpContext.Current.Server.MapPath("demo1.xml"));
  8. newElemment.Descendants("Color").Remove();//查询Color元素,并删除

newElemment.Descendants("Color").First().Value; //获取节点的值

三、XElement、XDocument 创建XML文档

  1. //1、 XElement 创建XML文档
  2. XElement doc = new XElement("ProductsParameter",new XAttribute("ID","1000"),new XElement("Products",new XElement("PoductType","DTSD342-9N"),new XElement("Queries","00020004")),new XElement("Variable",new XElement("Ua",new XElement("ID","1"),new XElement("Name","Ua"),new XElement("Description","A相电压"),new XElement("VType","DL/T 645-1997"),new XElement("Link","DL_0_0_DCBA"),new XElement("Formula","2#主变_DLT97_380.Ua}*{2#主变_DLT97_380.PTRatio}/10000"),new XElement("Unit","kv"),new XElement("Digit","3"),new XElement("ZeroFloat","0"),new XElement("InitValue",new XElement("MaxValue","10000000000"),new XElement("MinValue","-10000000000"),new XElement("IsRecord","True"),new XElement("RecordCycle","300"),new XElement("IsBroken","False")
  3. ))
  4. );
  5.  
  6. string path = Environment.CurrentDirectory;
  7. doc.Save(string.Format(@"{0}\ProductsXML\DTSD342-9N.xml",path));
  8.  
  9.  
  10. //2、XDocument 创建XML文档
  11. XDocument Xdoc = new XDocument(
  12. //XDeclaration 指定版本,编码
  13. new XDeclaration("1.0","utf-8","yes"),new XComment("注释!"),//XProcessingInstruction XML处理指令
  14. new XProcessingInstruction("xml-stylesheet","href='MyStyles.css' title='Compact' type='text/css'"),new XElement("Inventory",new XElement("Car",1),new XElement("Color","Green"),new XElement("Make","BMW")
  15. ),2),"Red"),"yugo"))
  16. )
  17. );
  18.  
  19. Xdoc.Save(string.Format(@"{0}\ProductsXML\Inventory.xml",path));

生成XML文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ProductsParameter ID="1000">
  3. <Products>
  4. <PoductType>DTSD342-9N</PoductType>
  5. <Queries>00020004</Queries>
  6. </Products>
  7. <Variable>
  8. <Ua>
  9. <ID>1</ID>
  10. <Name>Ua</Name>
  11. <Description>A相电压</Description>
  12. <VType>DL/T 645-1997</VType>
  13. <Link>DL_0_0_DCBA</Link>
  14. <Formula>2#主变_DLT97_380.Ua}*{2#主变_DLT97_380.PTRatio}/10000</Formula>
  15. <Unit>kv</Unit>
  16. <Digit>3</Digit>
  17. <ZeroFloat>0</ZeroFloat>
  18. <InitValue>0</InitValue>
  19. <MaxValue>10000000000</MaxValue>
  20. <MinValue>-10000000000</MinValue>
  21. <IsRecord>True</IsRecord>
  22. <RecordCycle>300</RecordCycle>
  23. <IsBroken>False</IsBroken>
  24. </Ua>
  25. </Variable>
  26. </ProductsParameter>

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <!--注释!-->
  3. <?xml-stylesheet href='MyStyles.css' title='Compact' type='text/css'?>
  4. <Inventory>
  5. <Car ID="1">
  6. <Color>Green</Color>
  7. <Make>BMW</Make>
  8. </Car>
  9. <Car ID="2">
  10. <Color>Red</Color>
  11. <Make>yugo</Make>
  12. </Car>
  13. </Inventory>

XDocument 与XElement 区别?

二者在通过Load方法加载XML时,你会发现二者的区别:

简单概括就是:

XDocument.Load() 加载整个XML文档 包括根节点

XElement.Load()不会加载XML的根节点

XElement.Load()示例代码

  1. File.WriteAllText("Test.xml",@"<Root>
  2. <Child1>1</Child1>
  3. <Child2>2</Child2>
  4. <Child3>3</Child3>
  5. </Root>");
  6.  
  7. Console.WriteLine("Querying tree loaded with XElement.Load");
  8. Console.WriteLine("----");
  9. XElement doc = XElement.Load("Test.xml");
  10. IEnumerable<XElement> childList =
  11. from el in doc.Elements()
  12. select el;
  13. foreach (XElement e in childList)
  14. Console.WriteLine(e);

结果:

  1. Querying tree loaded with XElement.Load
  2. ----
  3. <Child1>1</Child1>
  4. <Child2>2</Child2>
  5. <Child3>3</Child3>

XDocument.Load() 示例代码

  1. File.WriteAllText("Test.xml",@"<Root>
  2. <Child1>1</Child1>
  3. <Child2>2</Child2>
  4. <Child3>3</Child3>
  5. </Root>");
  6.  
  7. Console.WriteLine("Querying tree loaded with XDocument.Load");
  8. Console.WriteLine("----");
  9. XDocument doc = XDocument.Load("Test.xml");
  10. IEnumerable<XElement> childList =
  11. from el in doc.Elements()
  12. select el;
  13. foreach (XElement e in childList)
  14. Console.WriteLine(e);

结果:

  1. Querying tree loaded with XDocument.Load
  2. ----
  3. <Root>
  4. <Child1>1</Child1>
  5. <Child2>2</Child2>
  6. <Child3>3</Child3>
  7. </Root>

四、将数组、ADO.NET、文件数据转换成XML

1、数组

  1. //数组转换成XML
  2. static void MakeXElementFromArray()
  3. {
  4. var people = new[] {
  5. new {FirstName="张三",Age=20},new {FirstName="李四",Age=20}
  6. };
  7.  
  8. var arrayDataAsXElement = from c in people
  9. select
  10. new XElement("Person",new XAttribute("Age",c.Age),new XElement("FirstName",c.FirstName));
  11. XElement peopleDoc = new XElement("People",arrayDataAsXElement);
  12. Console.Write(peopleDoc);
  13. }


效果

  1. <People>
  2. <Person Age="20">
  3. <FirstName>张三</FirstName>
  4. </Person>
  5. <Person Age="20">
  6. <FirstName>李四</FirstName>
  7. </Person>
  8. </People>

猜你在找的XML相关文章