如何读写拥有命名空间xmlns 属性的Xml文件(C#实现)
我们在进行C#项目Xml读写开发时经常遇到一些读写问题,今天我要介绍的是遇到多个命名空间xmlns属性时如何读写此类文件。
比如下面这个Xml文件:
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<root>
<branch Name="Branch10" Value="abc">
<leaf Name="leaf11" Value="bcd" />
<leaf Name="leaf12" Value="cde" />
<leaf Name="leaf13" Value="def" />
</branch>
</root>
</project>
这个文件有一个默认的命名空间:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
还有两个拥有前缀x和d的命名空间:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
这类Xml文件的读写方法如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq; namespace ReadXml
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create("./test.xml"); xmlDoc.Load(reader);
reader.Close(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNamespaceManager.AddNamespace("e", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
xmlNamespaceManager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
xmlNamespaceManager.AddNamespace("d", "http://schemas.microsoft.com/expression/blend/2008"); //读xml
XmlNodeList nodeList = xmlDoc.SelectNodes("e:project/e:root/e:branch/e:leaf", xmlNamespaceManager);
foreach (XmlNode item in nodeList)
{
Console.WriteLine(item.OuterXml);
} //增加一条leaf
XmlNode xmlNode = xmlDoc.SelectSingleNode("e:project/e:root/e:branch", xmlNamespaceManager);
var ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; XmlElement leafElement = xmlDoc.CreateElement("leaf", ns); leafElement.SetAttribute("Name", "leaf14");
leafElement.SetAttribute("value", "efg"); xmlNode.AppendChild(leafElement); xmlDoc.Save("./test.xml"); Console.WriteLine("增加一条leaf后:"); XmlNodeList nodeListAdd = xmlDoc.SelectNodes("e:project/e:root/e:branch/e:leaf", xmlNamespaceManager); foreach (XmlNode item in nodeListAdd)
{
Console.WriteLine(item.OuterXml);
} //修改一条leaf
XmlNodeList nodeList1 = xmlDoc.SelectNodes("e:project/e:root/e:branch/e:leaf", xmlNamespaceManager); for (int i = (nodeList1.Count - 1); i >= 0; i--)
{
XmlElement xe = (XmlElement)nodeList1[i]; if (xe.GetAttribute("Name") == "leaf11")
{
xe.SetAttribute("Value", "aaa");
}
} xmlDoc.Save("./test.xml"); Console.WriteLine("修改第一条leaf后:");
XmlNodeList nodeListUpdate = xmlDoc.SelectNodes("e:project/e:root/e:branch/e:leaf", xmlNamespaceManager); foreach (XmlNode item in nodeListUpdate)
{
Console.WriteLine(item.OuterXml);
} Console.ReadKey();
}
}
}
程序运行后控制台显示如下:
<leaf Name="leaf11" Value="bcd" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf12" Value="cde" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf13" Value="def" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
增加一条leaf后:
<leaf Name="leaf11" Value="bcd" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf12" Value="cde" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf13" Value="def" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf14" value="efg" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
修改第一条leaf后:
<leaf Name="leaf11" Value="aaa" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf12" Value="cde" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf13" Value="def" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<leaf Name="leaf14" value="efg" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
新的Xml显示如下:
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<root>
<branch Name="Branch10" Value="abc">
<leaf Name="leaf11" Value="aaa" />
<leaf Name="leaf12" Value="cde" />
<leaf Name="leaf13" Value="def" />
<leaf Name="leaf14" value="efg" />
</branch>
</root>
</project>
部分启发来自于:https://www.programering.com/a/MjMyUDNwATk.html
如何读写拥有命名空间xmlns 属性的Xml文件(C#实现)的更多相关文章
- .NET:使用 XPATH 读取有 xmlns 属性的 XML 文档出现的问题
问题 xml <sqlMap namespace="WHTR.Dao.Accounts" xmlns="http://ibatis.apache.org/mappi ...
- java 编程基础:注解(Annotation Processing Tool)注解处理器 利用注解解读类属性生成XML文件
APT的介绍: APT(Annotation Processing Tool)是一种注解处理工具,它对源代码文件进行检测,并找出源文件所包含的注解信息,然后针对注解信息进行额外的处理. 使用APT工具 ...
- spring框架对于实体类复杂属性注入xml文件的配置
spring框架是javaWeb项目中至关重要的一个框架,大多web 项目在工作层次上分为持久层.服务层.控制层.持久层(dao.mapper)用于连接数据库,完成项目与数据库中数据的传递:服务层(s ...
- xml文件中 xmlns xmlns:xsi 等解释
http://bbs.csdn.NET/topics/390751819 maven 的 pom.xml 开头是下面这样的 <project xmlns="http://maven.a ...
- spring的显示装配bean(1)------通过XML文件装配
1:spring环境的简单搭建 (1)导入spring相关的jar包. 2:准备要进行装配的Java类 这里给出两个举例类 (1) (2) 3:配置XML文件 (1)在配置文件的顶部声明多个XML模式 ...
- Spring中Bean的配置:基于XML文件的方式
Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...
- C#创建XML文件并保存
随着XML的普及以及在动态WEB应用程序中大量应用,如何通过.NET创建,删除,修改XML文件变的也来也重要了.一个简单的概念是,XML文件跟大的文本文件并没有什么区别,同时它是先于.NET出现,很多 ...
- pom.xml文件
最近在了解maven创建的工程,拿到服务器的一段代码一直报错,是maven的pom.xml文件出错了,但是不知道是什么原因,所以就想知道pom.xml文件的作用及内容. 什么是POM? POM是项目对 ...
- C#_添加xml文件
引用:System.Xml; XmlDocument doc = new XmlDocument(); XmlElement Root = doc.CreateElement("Root&q ...
随机推荐
- 数据结构-kmp算法
定义 改进字符串的匹配算法 关键:通过实现一个包含了模式串的局部匹配信息的next()函数,利用匹配失败的信息,减少匹配次数. 1.BF算法 暴力匹配 给定 文本串S "BBC ABCDAB ...
- [CERC2014]Virus synthesis【回文自动机+DP】
[CERC2014]Virus synthesis 初始有一个空串,利用下面的操作构造给定串 SS . 1.串开头或末尾加一个字符 2.串开头或末尾加一个该串的逆串 求最小化操作数, \(|S| \l ...
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- Springboot 过滤器和拦截器详解及使用场景
一.过滤器和拦截器的区别 1.过滤器和拦截器触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的.请求结束返回也是,是在servlet处理完后,返回给前端之前. 2.拦截 ...
- Leetcode(215)-数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...
- 关于malloc/free用法
和很多人一样,我一直觉得new/delete和malloc/free的用法很随意,直到我真正遇到了麻烦,才想着去好好区分一下. (1)首先mallo函数原型void* malloc(size_t).头 ...
- Ubuntu桌面消失
Ubuntu桌面消失 按Ctrl + Alt + F1, 切回文本模式, 输入用户名和密码登录. 重装桌面和unity: sudo apt-get update sudo apt-get instal ...
- The best Fibonacci is achieved in js
The best Fibonacci is achieved in js the best realized by using js 斐波那契数列 "use strict"; /* ...
- TypeScript constructor public cause duplicate bug
TypeScript constructor public cause duplicate bug constructor public const log = console.log; // con ...
- fibonacci number & fibonacci sequence
fibonacci number & fibonacci sequence https://www.mathsisfun.com/numbers/fibonacci-sequence.html ...