zoukankan      html  css  js  c++  java
  • C# XML解析方式实例解析 (带命名空间的)

    http://www.csharpwin.com/csharpspace/3968r4687.shtml

     C# XML解析通过XPath的方式的步骤:

        1、需要先加载文档,然后再读取想要的节点值。

        ◆xml文档

        protected XmlDocument doc = null;

        ◆xml文档的根元素(节点)

        protected XmlElement root = null;

        ◆xml文档的名空间管理器

        protected XmlNamespaceManager nsmgr = null;

        2、接下来就是加载文档了

    1. protected void LoadXmlFile(FileInfo xmlFile)
    2. {
    3. if (xmlFile == null || !xmlFile.Exists)
    4. {
    5. throw new FileNotFoundException(
    6. string.Format("要解析的文件不存在{0}。",
    7. xmlFile.FullName));
    8. }
    9. //加载文件
    10. this.doc = new XmlDocument();
    11. doc.Load(xmlFile.FullName);
    12. //准备读取文件
    13. root = doc.DocumentElement;
    14. string nameSpace = root.NamespaceURI;
    15. nsmgr = new XmlNamespaceManager(doc.NameTable);
    16. nsmgr.AddNamespace("ns", nameSpace);
    17. }

        ◆C# XML解析通过XPath的方式要注意。

        a、这两行是取得xml文档的名空间

    1. root = doc.DocumentElement;
    2. string nameSpace = root.NamespaceURI;

        b、这两行是建立xml文档的名空间管理器

    1. nsmgr = new XmlNamespaceManager(doc.NameTable);
    2. nsmgr.AddNamespace("ns", nameSpace);

        如果你的xml文档有名空间,则这部分的代码是必不可少的。

        3、接下来就是读取文档节点的值了

        这里两个传入参数prefixPath是节点的上级节点路径,xRelativePath是要读取的节点名称。

        另外,变量XmlFileInfo是要加载的xml文件。

    1. protected string GetNodeValue(
    2. string prefixPath, string xRelativePath)
    3. {
    4. if (doc == null)
    5. {
    6. LoadXmlFile(XmlFileInfo);
    7. }
    8. string xPath = string.Empty;
    9. if (!string.IsNullOrEmpty(xRelativePath))
    10. {
    11. if (!string.IsNullOrEmpty(prefixPath))
    12. {
    13. xPath = prefixPath + xRelativePath;
    14. }
    15. else
    16. {
    17. xPath = xRelativePath;
    18. }
    19. }
    20. xPath = xPath.Replace("/", "/ns:");
    21. XmlNode node = root.SelectSingleNode(xPath, nsmgr);
    22. if (node == null)
    23. {
    24. return null;
    25. }
    26. return node.InnerXml;
    27. }

        可能有的朋友要问,为什么要设置两个参数prefixPath和xRelativePath呢,其实这个没有多大的关系,我只是为了自己觉得方便,你也可以在方法外确定了这个XPath,在方法中只设置一个传入参数,效果是一样的。

        ◆注意这一行:

    1. xPath = xPath.Replace("/", "/ns:");

        如果你的xml文档带名空间,则这行是比不可少的,否则会出现找不到节点,无法解析的情况。

        关于XPath的一些问题:

        对于这样一个xml文档,要查找第一个节点下的学生的Name时(ID=01),其XPath应该是"/ns:Root/ns:Students/ns:Student[1]/ns:Name"。xml对于重复的节点名称,是按照顺序1,2,3...的方式遍历的,也就是说如果要找第N个Student节点的下的节点之,那么应使用Student[N]的标识方式。

    1. ﹤?xml version="1.0" encoding="UTF-8" ?﹥
    2. ﹤Root xmlns="urn:ClassNameSpace"
    3. ﹤Class﹥
    4. ﹤ClassID﹥1234﹤/ClassID﹥
    5. ﹤/Class﹥
    6. ﹤Students﹥
    7. ﹤Student﹥
    8. ﹤ID﹥01﹤/ID﹥﹤Name﹥Name01﹤/Name﹥
    9. ﹤/Student﹥
    10. ﹤Student﹥
    11. ﹤ID﹥02﹤/ID﹥﹤Name﹥Name02﹤/Name﹥
    12. ﹤/Student﹥
    13. ﹤/Students﹥
    14. ﹤/Root﹥

        当然,这里也可以获取节点属性的值,查找满足特定值的节点等等,这些和上面获取节点值的过程是类似的。

        C# XML解析通过XPath的方式的实现就向你介绍到这里,希望对你了解和学习C# XML解析有所帮助。

  • 相关阅读:
    postgres 输出数据集的自定义函数
    python 验证码识别初探
    python 位运算【实测】
    postgresql 安装插件
    pyasn1 安装异常
    K&R C Note
    如何安全地删除固态硬盘(SSD)、U盘等闪存设备上的文件使其无法恢复?
    C语言枚举类型的使用及其优越性
    算法设计之简易模拟推特(数据结构的综合练习)
    UML笔记之类图
  • 原文地址:https://www.cnblogs.com/quietwalk/p/2811620.html
Copyright © 2011-2022 走看看