C#使用xmlDocument类操作非标准的xml文件,应该先引用它的命名空间

如:

<Workbook xmlns:html="urn:schemas-microsoft-com:office:spreadsheet">

  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">

    <WindowHeight>7995</WindowHeight>

    <WindowWidth>14955</WindowWidth>

  </ExcelWorkbook>

</Workbook>

XmlDocument xmlDoc = new XmlDocument();

xml.Load(@"D:\MyDocuments\VSS\Bonus.root\Bonus\TestDB\YearBonus.xml");

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);

nsmgr.AddNamespace("ab", "urn:schemas-microsoft-com:office:spreadsheet");//引用命名空间

XmlNode root = xml.DocumentElement;

XmlNode v1 = root.SelectSingleNode("//ab:Workbook/ab:ExcelWorkbook/ab:WindowHeight", nsmgr)//按照顺序读取节点

SMIL类型的文件

<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">   <head>     <meta name="title" content="smil"/>     <layout>       <root-layout width="100%" height="100%"/>       <region id="Image" left="0%" top="0%" height="75%" width="100%" fit="meet"/>       <region id="Text" left="0%" top="0%" height="25%" width="100%" fit="scroll"/>     </layout>   </head>   <body>     <par>       <text src="0d9e3743-9909-45e6-8d62-e28ea29e8e35.txt" region="Text"/>     </par>     <par>       <img src="26495120-e3fa-47b3-b64b-a66a138da120.gif" region="Image"/>     </par>   </body> </smil>

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(sFilePath); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("default", "http://www.w3.org/2000/SMIL20/CR/Language");//引用命名空间 XmlNode mNode = xmlDoc.SelectSingleNode("//default:smil/default:body", nsmgr);//按照顺序读取 for (int i = 0; i < mNode.ChildNodes.Count; i++) {       Response.Write(mNode.ChildNodes[i].FirstChild.Attributes["region"].Value); }

posted on 2011-11-16 18:16  晓晨  阅读(1197)  评论(0编辑  收藏  举报