如何讀寫擁有命名空間xmlns 屬性的Xml文件(C#實現)

        咱們在進行C#項目Xml讀寫開發時常常遇到一些讀寫問題,今天我要介紹的是遇到多個命名空間xmlns屬性時如何讀寫此類文件。html

  好比下面這個Xml文件:node

<?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>

  這個文件有一個默認的命名空間:express

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        還有兩個擁有前綴x和d的命名空間:spa

 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(); } } }

  程序運行後控制檯顯示以下:code

<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

<?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.htmlhtm

相關文章
相關標籤/搜索