熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

創建、查詢、修改帶名稱空間的XML文件

2022-06-13   來源: .NET編程 
     XML的應用越來越廣泛了如VistaFlex編程都將使用 XML正確掌握XML的各種操作對提高編程效率至關重要下面就是一個綜合處理帶名稱空間的XML的例子
  
  C#
  
  string wNameSpace = ;
  SystemXmlXmlDocument doc = new SystemXmlXmlDocument();
  
  //創建根節點
  SystemXmlXmlNode root = docCreateNode(SystemXmlXmlNodeTypeElement w wordDocument );
  SystemXmlXmlAttribute xa;
  xa = docCreateAttribute(xmlns v wNameSpace);
  xaValue = urn:schemasmicrosoftcom:vml;
  rootAttributesAppend(xa);
  
  //為節點添加屬性
  xa = docCreateAttribute(xmlns w wNameSpace);
  xaValue = urn:schemasmicrosoftcom:office:word;
  rootAttributesAppend(xa);
  
  xa = docCreateAttribute(xmlns SL wNameSpace);
  xaValue = ;
  rootAttributesAppend(xa);
  
  xa = docCreateAttribute(xmlns aml wNameSpace);
  xaValue = ;
  rootAttributesAppend(xa);
  
  xa = docCreateAttribute(xmlns wx wNameSpace);
  xaValue = ;
  rootAttributesAppend(xa);
  
  xa = docCreateAttribute(xmlns o wNameSpace);
  xaValue = urn:schemasmicrosoftcom:office:office;
  rootAttributesAppend(xa);
  
  xa = docCreateAttribute(xmlns dt wNameSpace);
  xaValue = uuid:CFBdAFAAC;
  rootAttributesAppend(xa);
  
  xa = docCreateAttribute(xmlns space wNameSpace);
  xaValue = preserve;
  rootAttributesAppend(xa);
  
  //為節點增加值
  SystemXmlXmlNode body = docCreateNode(SystemXmlXmlNodeTypeElement v body urn:schemasmicrosoftcom:vml);
  SystemXmlXmlNode childNode = docCreateNode(SystemXmlXmlNodeTypeElement o t urn:schemasmicrosoftcom:office:office);
  childNodeInnerText = 歡迎光臨【孟憲會之精彩世界】;
  
  //添加到內存樹中
  bodyAppendChild(childNode);
  rootAppendChild(body);
  docAppendChild(root);
  
  //添加節點聲明
  SystemXmlXmlDeclaration xd = docCreateXmlDeclaration( UTF yes);
  docInsertBefore(xd docDocumentElement);
  
  //添加處理指令
  SystemXmlXmlProcessingInstruction spi = docCreateProcessingInstruction(msoapplication progid=\WordDocument\);
  docInsertBefore(spi docDocumentElement);
  
  //查詢節點
  SystemXmlXmlNamespaceManager nsmanager = new SystemXmlXmlNamespaceManager(docNameTable);
  nsmanagerAddNamespace(w );
  nsmanagerAddNamespace(v urn:schemasmicrosoftcom:vml);
  nsmanagerAddNamespace(o urn:schemasmicrosoftcom:office:office);
  SystemXmlXmlNode node = docSelectSingleNode(w:wordDocument/v:body/o:t nsmanager);
  ResponseWrite(nodeInnerText);
  
  nodeInnerText = 歡迎光臨【孟憲會之精彩世界】:/;
  
  //創建CDATA節點
  SystemXmlXmlCDataSection xcds = docCreateCDataSection(<a >【孟憲會之精彩世界】</a>);
  nodeParentNodeInsertAfter(xcds node);
  
  ResponseWrite(xcdsInnerText);
  
  docSave(ServerMapPath(testxml));
From:http://tw.wingwit.com/Article/program/net/201311/13631.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.