如何读写拥有命名空间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 ...
随机推荐
- 阅读笔记:ImageNet Classification with Deep Convolutional Neural Networks
概要: 本文中的Alexnet神经网络在LSVRC-2010图像分类比赛中得到了第一名和第五名,将120万高分辨率的图像分到1000不同的类别中,分类结果比以往的神经网络的分类都要好.为了训练更快,使 ...
- Codeforces Round #602 Div2 D1. Optimal Subsequences (Easy Version)
题意:给你一个数组a,询问m次,每次返回长度为k的和最大的子序列(要求字典序最小)的pos位置上的数字. 题解:和最大的子序列很简单,排个序就行,但是题目要求字典序最小,那我们在刚开始的时候先记录每个 ...
- Win10 资源管理器经常卡死问题
什么问题? 我的Win10资源管理器开始出现卡死(假死.未响应)的情况,频率越来越高,触发方式越来越广,包括OpenDialog.打开拥有快捷方式的文件夹/桌面.右键空白处.右键文件/文件夹.拖动文件 ...
- constexpr 的来龙去脉
constexpr 是什么? 关键字 constexpr (constant expression) 是在 C++11 中引入的,并且在 C++14 中进行了优化. constexpr 和 const ...
- docker-swarm----多机容器管理
Docker Swarm: 准备三台机器,都装上 Docker docker swarm是docker官方提供的一套容器编排系统.它的架构如下: swarm是一系列节点的集合,而节点可以是一台裸机或者 ...
- KafkaBroker 简析
Kafka 依赖 Zookeeper 来维护集群成员的信息: Kafka 使用 Zookeeper 的临时节点来选举 controller Zookeeper 在 broker 加入集群或退出集群时通 ...
- 内存耗尽后Redis会发生什么
前言 作为一台服务器来说,内存并不是无限的,所以总会存在内存耗尽的情况,那么当 Redis 服务器的内存耗尽后,如果继续执行请求命令,Redis 会如何处理呢? 内存回收 使用Redis 服务时,很多 ...
- MDK5生成BIn文件的方法
配置MDK5 生成bin文件的 第一步:方法打开option for Target 第二步:选择 user 第三步:找到After Build/Rebuild 第四步:勾选run,点击文件选择小图标选 ...
- Windows中VS code无法查看C++ STL容器的值 - 解决方法
Windows中VS code debug时无法查看C++ STL容器内容 首先,你很可能用的是x64版本的Windows. 我发现一个有效的解决方法,但在x64版本的Windows上安装MinGW时 ...
- hihoCoder Challenge 2
#1046 : K个串 时间限制:40000ms 单点时限:2000ms 内存限制:1024MB 描述 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计 ...