代码改变世界

c#加载XML文档

2018-06-11 18:09  bsdbqling  阅读(255)  评论(0)    收藏  举报

XmlDocument 类

表示 XML 文档。 可使用此类在文档中加载、验证、编辑、添加和放置 XML。

命名空间:   System.Xml
程序集:  System.Xml(位于 System.Xml.dll)

System.Object
  System.Xml.XmlNode
    System.Xml.XmlDocument
      System.Configuration.ConfigXmlDocument
      System.Xml.XmlDataDocument
用法:
 
System_CAPS_pubmethod Load(String)

从指定的 URL 加载 XML 文档。

System_CAPS_pubmethod Load(TextReader)

从指定的 TextReader 加载 XML 文档。

例子:

System.Xml.XmlDocument  doc = new System.Xml.Xmldocument();
doc.Load(文件路径)
参考https://www.cnblogs.com/bsdbqling/p/9167886.html

 

用法:

System_CAPS_pubmethod SelectSingleNode(String)

选择第一个 XmlNode ,与 XPath 表达式匹配。(继承自 XmlNode。)

例子:
XmlNode nd = doc.SelectSingleNode("Models/Model[@id='"+infoType+"']");

 

 用法:

程序集:  System.Xml(位于 System.Xml.dll)

public virtual XmlAttributeCollection Attributes { get; }

属性值

Type: System.Xml.XmlAttributeCollection

XmlAttributeCollection 包含节点的属性。

如果节点为 XmlNodeType.Element 类型,则返回该节点的属性。 否则,此属性将返回 null

例子:

nd.Attributes["exttable"].value;

 nd.Attributes["filed"].value;

附加文件如下:

string tempFileName = AppDomain.CurrentDomain.BaseDirectory + "temp\\" + Guid.NewGuid().ToString() + ".docx";
string modelPath = AppDomain.CurrentDomain.BaseDirectory + "Model\\WordModel\\InfoImpModel.docx";
string configFile = AppDomain.CurrentDomain.BaseDirectory + "Configure\\Bizz\\ExpConfig.xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(configFile);
XmlNode nd = doc.SelectSingleNode("Models/Model[@id='" + infoType + "']");
XmlNode mbnd = doc.SelectSingleNode("Models/Model[@id='" + infoType + "']/BookMark[@name='详细内容']");
string extTable = nd.Attributes["exttable"].Value;
string contentField = mbnd.Attributes["filed"].Value;