首页 > 代码库 > c#读取带有命名空间的XML

c#读取带有命名空间的XML

XML文件内容

<?xml version="1.0" standalone="no"?><svg width="1709px" height="886px"  onl oad="init(evt)"  viewBox="0 0 1800 900" xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">   <rect width="1709px" height="886px" fill="GradientInactiveCaption" />   <rect x="88" y="369" width="1200px" height="15px" stroke-width="1" stroke="Black" fill="url(#79)" />   <rect id="REED.ShapeElement.TrapezoidR" x="491" y="283" width="80px" height="185px" stroke-width="1" stroke="Transparent" fill="Transparent" /></svg>

以下是读取方法:

class Program    {        static void Main(string[] args)        {            try            {                XmlDocument xmlDoc = new XmlDocument();                 xmlDoc.Load("F:\\汽机本体2.svg");                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);                nsmgr.AddNamespace("ns", "http://www.w3.org/2000/svg");                XmlNode xn = xmlDoc.SelectSingleNode("//ns:svg", nsmgr);                XmlNodeList xnl = xn.ChildNodes;                Console.WriteLine("读取成功");                Console.ReadLine();            }            catch (Exception ex)            {                Console.WriteLine(ex.ToString());                Console.ReadLine();                throw;            }                    }    }