首页 > 其他分享 >LINQ TO XML

LINQ TO XML

时间:2023-01-09 15:37:57浏览次数:34  
标签:XML XElement void LINQ Element new root


LINQ to XML 提供了改进的 XML 编程接口,这一点可能与 LINQ to XML 的 LINQ 功能同样重要。 通过 LINQ to XML,对 XML 编程时,您可以实现任何预期的操作,包括:


  • 从文件或流加载 XML。
  • 将 XML 序列化为文件或流。
  • 使用函数构造从头开始创建 XML。
  • 使用类似 XPath 的轴查询 XML。
  • 使用 Add、Remove、ReplaceWith 和 SetValue 等方法对内存 XML 树进行操作。
  • 使用 XSD 验证 XML 树。
  • 使用这些功能的组合,可将 XML 树从一种形状转换为另一种形状。




1、读取XML文档


   从文件中加载:

XElement root = XElement.Load(path);



从内存中加载:

XElement root = XElement.Parse(@"

<Categories>

<Category>

<CategoryID>1</CategoryID>

<CategoryName>Beverages</CategoryName>

<Description>Soft drinks, coffees, teas, beers, and ales</Description>

</Category>

</Categories>

");

2、基本操作:添加、删除、修改、保存节点操作


 添加:

public static void AddAfterSelf()
{
XElement root = XElement.Parse(@"

<Categories>

<Category>

<CategoryID>1</CategoryID>

<CategoryName>Beverages</CategoryName>

<Description>Soft drinks, coffees, teas, beers, and ales</Description>

</Category>

</Categories>

");
XElement xele = root.Element("Category").Element("CategoryName");
xele.AddAfterSelf(new XElement("AddDate", DateTime.Now));
root.Save(path);
}

修改:

public static void Update()
public static void Update()
{
XElement root = XElement.Parse(@"
<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks, coffees, teas, beers, and ales</Description>
</Category>
</Categories>
");
root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID", "2"));
root.Element("Category").SetElementValue("CategoryName", "test data");
root.Save(path);
}

删除:

public static void Remove()
{
string path = @"d:\";
XElement root = XElement.Parse(@"
<Categories>

<Category>

<CategoryID>1</CategoryID>

<CategoryName>Beverages</CategoryName>

<Description>Soft drinks, coffees, teas, beers, and ales</Description>

</Category>

</Categories>

");
root.RemoveAll();
root.Save(path);
}

添加属性

public static void AddAttribute()
{
XElement root = new XElement("Categories",
new XElement("Category",
new XAttribute("CategoryID", "1"),
new XElement("CategoryName", "Beverages"),
new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
)
);
root.Element("Category").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString()));
root.Save(path);
}

3、查询语法


检索属性:

public static void SelectAttribute()
{
XElement root = new XElement("Categories",
new XElement("Category",
new XAttribute("CategoryID", "1"),
new XElement("CategoryName", "Beverages"),
new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
)
);
XAttribute xattr = root.Element("Category").Attribute("CategoryID");
Console.WriteLine(xattr.Name);
Console.WriteLine(xattr.Value);
}

遍历:

public static void Enum()
public static void Enum()
public static void Enum()
{
XElement root = new XElement("Categories");
using (NorthwindDataContext db = new NorthwindDataContext())
{
root.Add(
db.Categories
.Select
(
c => new XElement

(
"Category"

, new XElement("CategoryName", c.CategoryName)
)
)
);
}
foreach (var item in root.Elements("Category"))
{
Console.WriteLine(item.Element("CategoryName").Value);
}
}

4、加载复杂对象

         

var rooms = from room in doc.Descendants("Room")
select new
{
RoomID = room.Element("RoomID").Value,
Prices = from price in XDocument.Parse(room.ToString()).Descendants("Price")
select new
{
PriceCode = price.Element("PriceCode").Value,
Stays = from stay in XDocument.Parse(price.ToString()).Descendants("Stay")
select new
{
Date = stay.Element("Date").Value,
Rate = stay.Element("Rate").Value,
RoomStatus = stay.Element("RoomStatus").Value,
BreakfastType = stay.Element("BreakfastType").Value
}
}
};

标签:XML,XElement,void,LINQ,Element,new,root
From: https://blog.51cto.com/u_15870687/5997290

相关文章

  • java根据xml节点地址获取指定节点内容
    备好几个前同事问过怎么获取xml指定节点内容后,终于决定写个工具类,今天特地分享给大家,写的不好,不要喷maven依赖包<dependency><groupId>dom4j</groupId><artifactId>do......
  • Linq学习总结
    Linq可以对字符串、集合等“结果集”通过扩展方法,进行过滤、排序、分组、计算等操作。学习Linq,需要需要了解委托delegate以及委托的语法糖Action和Func。Action和Func经过......
  • pom.xml经常用到去写的坐标
    tomcat7插件1<plugin>2<groupId>org.apache.tomcat.maven</groupId>3<artifactId>tomcat7-maven-plugin</artifactId>4</plugin>jdk17坐标,因......
  • C#-XML文件及字符串的加密解密代码
    先看XML文件的加解密,例如在桌面有个Student.xml文档,包含学生信息,需进行加密处理:开始代码,添加命名空间引用:usingSystem.Security.Cryptography;usingSystem.Security.......
  • Spring5 IOC容器解析——XML配置的资源定位、加载、解析、注册分析
    从FileSystemXmlApplicationContext开始ApplicationContextapplicationContext=newFileSystemXmlApplicationContext(xmlPath);由上面的入口进入到构造方法中public......
  • lxml与XPath
    lxml与XPath​ 尽管正则表达式处理字符串的能力非常强,但编写功能强大的正则表达式并不容易,而且难以维护,复杂的正则表达式也并不容易理解​ 幸好还有其他的方式处理字符串......
  • XML简单了解
    XML一、XML基础1、XML概述:​ XML的全称为(ExtensibleMarkupLanguage),是一种可扩展的标记语言​ 标记语言:通过标签来描述数据的一门语言(标签有时我......
  • Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310
    Causedby:java.lang.NoClassDefFoundError:com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerializer 1、报错信息SpringBoot项目启动的时候,报错如下:Caused......
  • [记]python操作xml文件
    test.xml<A><AA><AAAname="aaa">AaA</AAA><BBB></BBB></AA><BB><CCCname="ccc">CcC</CCC><DDD></DDD></BB&g......
  • java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalo
    一、问题复现这个问题困扰我一天多,各种百度看论坛,发现有一种解决方案可能对我有帮助(解决方案)解决方案说的是可能是xerces包冲突所致Causedby:java.lang.AbstractMe......