我们在进行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#实现)的更多相关文章

  1. .NET:使用 XPATH 读取有 xmlns 属性的 XML 文档出现的问题

    问题 xml <sqlMap namespace="WHTR.Dao.Accounts" xmlns="http://ibatis.apache.org/mappi ...

  2. java 编程基础:注解(Annotation Processing Tool)注解处理器 利用注解解读类属性生成XML文件

    APT的介绍: APT(Annotation Processing Tool)是一种注解处理工具,它对源代码文件进行检测,并找出源文件所包含的注解信息,然后针对注解信息进行额外的处理. 使用APT工具 ...

  3. spring框架对于实体类复杂属性注入xml文件的配置

    spring框架是javaWeb项目中至关重要的一个框架,大多web 项目在工作层次上分为持久层.服务层.控制层.持久层(dao.mapper)用于连接数据库,完成项目与数据库中数据的传递:服务层(s ...

  4. xml文件中 xmlns xmlns:xsi 等解释

    http://bbs.csdn.NET/topics/390751819 maven 的 pom.xml 开头是下面这样的 <project xmlns="http://maven.a ...

  5. spring的显示装配bean(1)------通过XML文件装配

    1:spring环境的简单搭建 (1)导入spring相关的jar包. 2:准备要进行装配的Java类 这里给出两个举例类 (1) (2) 3:配置XML文件 (1)在配置文件的顶部声明多个XML模式 ...

  6. Spring中Bean的配置:基于XML文件的方式

    Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...

  7. C#创建XML文件并保存

    随着XML的普及以及在动态WEB应用程序中大量应用,如何通过.NET创建,删除,修改XML文件变的也来也重要了.一个简单的概念是,XML文件跟大的文本文件并没有什么区别,同时它是先于.NET出现,很多 ...

  8. pom.xml文件

    最近在了解maven创建的工程,拿到服务器的一段代码一直报错,是maven的pom.xml文件出错了,但是不知道是什么原因,所以就想知道pom.xml文件的作用及内容. 什么是POM? POM是项目对 ...

  9. C#_添加xml文件

    引用:System.Xml; XmlDocument doc = new XmlDocument(); XmlElement Root = doc.CreateElement("Root&q ...

随机推荐

  1. URAL - 1635 哈希区间(或者不哈希)+dp

    题意: 演队在口试中非常不幸.在42道考题中,他恰好没有准备最后一道题,而刚好被问到的正是那道题.演队坐在教授面前,一句话也说不出来.但教授心情很好,给了演队最后一次通过考试的机会.他让这个可怜的学生 ...

  2. Linux-单用户/救援模式

    目录 企业案例一:忘记root密码 企业案例二:修改了默认的运行级别为poweroff或者reboot 企业案例三:误损坏MBR(只能以救援模式解决) 企业案例四:误删除GRUB菜单(只能以救援模式解 ...

  3. 使用DTK创建模糊背景窗口并自定义阴影效果

    DTK是deepin开发的基于Qt的开发套件,提供了大量的具有独特风格的美化控件,也提供了很多非常方便的API,下边我们用DTK实现一个模糊窗口,并设置其阴影效果. 使用场景 一切需要模糊窗口作为美化 ...

  4. 关于malloc/free用法

    和很多人一样,我一直觉得new/delete和malloc/free的用法很随意,直到我真正遇到了麻烦,才想着去好好区分一下. (1)首先mallo函数原型void* malloc(size_t).头 ...

  5. bzoj5312 冒险(吉司机线段树)题解

    题意: 已知\(n\)个数字,进行以下操作: \(1.\)区间\([L,R]\) 按位与\(x\) \(2.\)区间\([L,R]\) 按位或\(x\) \(3.\)区间\([L,R]\) 询问最大值 ...

  6. HDU 6706 huntian oy(杜教筛 + 一些定理)题解

    题意: 已知\(f(n,a,b)=\sum_{i=1}^n\sum_{j=1}^igcd(i^a-j^a,i^b-j^b)[gcd(i,j)=1]\mod 1e9+7\),\(n\leq1e9\),且 ...

  7. HDU 4746 Mophues(莫比乌斯反演)题解

    题意: \(Q\leq5000\)次询问,每次问你有多少对\((x,y)\)满足\(x\in[1,n],y\in[1,m]\)且\(gcd(x,y)\)的质因数分解个数小于等于\(p\).\(n,m, ...

  8. MDK5生成BIn文件的方法

    配置MDK5 生成bin文件的 第一步:方法打开option for Target 第二步:选择 user 第三步:找到After Build/Rebuild 第四步:勾选run,点击文件选择小图标选 ...

  9. 图片转tfrecords

    import numpy as np import tensorflow as tf import time import os import cv2 from sklearn.utils impor ...

  10. how to config custom process.env in node.js

    how to config custom process.env in node.js process.env APP_ENV NODE_ENV https://nodejs.org/api/proc ...