加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

怎么将XML字符串转换为XmlDocument,并获取部分节点值

发布时间:2020-12-16 08:55:50 所属栏目:asp.Net 来源:网络整理
导读:我们的目标:解析XML字符串,并获取节点:z:row下的属性:ows_LinkFilename的值集合??? ? XML字符串数据结构如下: listitems xmlns:s ='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' xmlns:dt ='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:rs ='

我们的目标:解析XML字符串,并获取节点:z:row下的属性:ows_LinkFilename的值集合???

?

XML字符串数据结构如下:

<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema' xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
rs:data ItemCount='5'>
   z:row ows_DocIcon='docx' ows_LinkFilename='RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx' ows_Modified='2014-03-31 09:54:04' ows_Editor='7437;#(已离职)A-张强' ows_MetaInfo='6;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:SW|路平 _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang ' ows__ModerationStatus='0' ows__Level='1' ows_Last_x0020_Modified='6;#2014-03-31 09:54:04' ows_ID='6' ows_owshiddenversion ows_UniqueId='6;#{DF5527D4-F122-465D-B9F2-07887D317D9B}' ows_FSObjType='6;#0' ows_Created_x0020_Date ows_ProgId='6;#' ows_FileLeafRef='6;#RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx' ows_FileRef='6;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx' />
   ='RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx'='7;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:EW| _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang '='7;#2014-03-31 09:54:05'='7'='7;#{94DFC367-99A6-4A64-8CB2-D398ECAB0CA7}'='7;#0'='7;#'='7;#RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx'='7;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx' ='RL-RYTG-V1.0-BZ.003_录用管理标准.docx'='8;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:SW|路平 _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang '='8;#2014-03-31 09:54:05'='8'='8;#{1B24DB6A-47E0-4827-BAA9-EB2B3F3294C3}'='8;#0'='8;#'='8;#RL-RYTG-V1.0-BZ.003_录用管理标准.docx'='8;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-BZ.003_录用管理标准.docx' ='RL-RYTG-V1.0-GF.001_内部应聘规范.docx'='9;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:EW| _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang '='9;#2014-03-31 09:54:05'='9'='9;#{BD7529F5-DAB2-4C35-9DEC-C8F83013336B}'='9;#0'='9;#'='9;#RL-RYTG-V1.0-GF.001_内部应聘规范.docx'='9;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-GF.001_内部应聘规范.docx' ='RL-RYTG-V1.0-GF.002_内部推荐规范.docx'='2014-03-31 09:54:05'='10;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:EW| _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang '='10;#2014-03-31 09:54:05'='10'='10;#{9B2ECE9E-4544-476C-BD32-7182CBBF49FE}'='10;#0'='10;#'='10;#RL-RYTG-V1.0-GF.002_内部推荐规范.docx'='10;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-GF.002_内部推荐规范.docx' />
</rs:datalistitems>

C#代码如下:

string xmlStr = "<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema' xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
            xmlStr += <rs:data ItemCount='5'><z:row ows_DocIcon='docx' ows_LinkFilename='RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx' ows_Modified='2014-03-31 09:54:04' ows_Editor='7437;#(已离职)A-张强' ows_MetaInfo='6;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:SW|路平 _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang ' ows__ModerationStatus='0' ows__Level='1' ows_Last_x0020_Modified='6;#2014-03-31 09:54:04' ows_ID='6' ows_owshiddenversion='1' ows_UniqueId='6;#{DF5527D4-F122-465D-B9F2-07887D317D9B}' ows_FSObjType='6;#0' ows_Created_x0020_Date='6;#2014-03-31 09:54:04' ows_ProgId='6;#' ows_FileLeafRef='6;#RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx' ows_FileRef='6;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx' /><z:row ows_DocIcon='docx' ows_LinkFilename='RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx' ows_Modified='2014-03-31 09:54:04' ows_Editor='7437;#(已离职)A-张强' ows_MetaInfo='7;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:EW| _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang ' ows__ModerationStatus='0' ows__Level='1' ows_Last_x0020_Modified='7;#2014-03-31 09:54:05' ows_ID='7' ows_owshiddenversion='1' ows_UniqueId='7;#{94DFC367-99A6-4A64-8CB2-D398ECAB0CA7}' ows_FSObjType='7;#0' ows_Created_x0020_Date='7;#2014-03-31 09:54:05' ows_ProgId='7;#' ows_FileLeafRef='7;#RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx' ows_FileRef='7;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx' /><z:row ows_DocIcon='docx' ows_LinkFilename='RL-RYTG-V1.0-BZ.003_录用管理标准.docx' ows_Modified='2014-03-31 09:54:04' ows_Editor='7437;#(已离职)A-张强' ows_MetaInfo='8;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:SW|路平 _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang ' ows__ModerationStatus='0' ows__Level='1' ows_Last_x0020_Modified='8;#2014-03-31 09:54:05' ows_ID='8' ows_owshiddenversion='1' ows_UniqueId='8;#{1B24DB6A-47E0-4827-BAA9-EB2B3F3294C3}' ows_FSObjType='8;#0' ows_Created_x0020_Date='8;#2014-03-31 09:54:05' ows_ProgId='8;#' ows_FileLeafRef='8;#RL-RYTG-V1.0-BZ.003_录用管理标准.docx' ows_FileRef='8;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-BZ.003_录用管理标准.docx' /><z:row ows_DocIcon='docx' ows_LinkFilename='RL-RYTG-V1.0-GF.001_内部应聘规范.docx' ows_Modified='2014-03-31 09:54:04' ows_Editor='7437;#(已离职)A-张强' ows_MetaInfo='9;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:EW| _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang ' ows__ModerationStatus='0' ows__Level='1' ows_Last_x0020_Modified='9;#2014-03-31 09:54:05' ows_ID='9' ows_owshiddenversion='1' ows_UniqueId='9;#{BD7529F5-DAB2-4C35-9DEC-C8F83013336B}' ows_FSObjType='9;#0' ows_Created_x0020_Date='9;#2014-03-31 09:54:05' ows_ProgId='9;#' ows_FileLeafRef='9;#RL-RYTG-V1.0-GF.001_内部应聘规范.docx' ows_FileRef='9;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-GF.001_内部应聘规范.docx' /><z:row ows_DocIcon='docx' ows_LinkFilename='RL-RYTG-V1.0-GF.002_内部推荐规范.docx' ows_Modified='2014-03-31 09:54:05' ows_Editor='7437;#(已离职)A-张强' ows_MetaInfo='10;#Subject:SW|远洋地产控股有限公司流程 Keywords:EW| _Status:EW| vti_parserversion:SR|12.0.0.6421 vti_modifiedby:SR|SINOOCEANLANDv-zhangqiang vti_title:EW| _Author:EW| _Category:EW| ContentType:EW| ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8 _Comments:EW| vti_author:SR|SINOOCEANLANDv-zhangqiang ' ows__ModerationStatus='0' ows__Level='1' ows_Last_x0020_Modified='10;#2014-03-31 09:54:05' ows_ID='10' ows_owshiddenversion='1' ows_UniqueId='10;#{9B2ECE9E-4544-476C-BD32-7182CBBF49FE}' ows_FSObjType='10;#0' ows_Created_x0020_Date='10;#2014-03-31 09:54:05' ows_ProgId='10;#' ows_FileLeafRef='10;#RL-RYTG-V1.0-GF.002_内部推荐规范.docx' ows_FileRef='10;#sites/ServiceBiz/Service25/4/1/RL-RYTG-V1.0-GF.002_内部推荐规范.docx' /></rs:data></listitems>;
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlStr);
            XmlNodeList listitemsNodeList = xmlDocument.GetElementsByTagName(rs:data);
            XmlNodeList dataNodeList = listitemsNodeList[0].ChildNodes;
            List<string> result = new List<string>();
            foreach (XmlNode node in dataNodeList)
            {
                string fileName = node.Attributes[ows_LinkFilename].Value;
                result.Add(fileName);
            }
            result.ForEach(re =>
            {
                Console.WriteLine(re);
            });
            Console.ReadLine();

结果图如下:

?

总结关键知识点:

1-如何将XML字符串转换为XmlDocdument:

  XmlDocument xmlDocument = new XmlDocument();

  xmlDocument.LoadXml(xmlStr);

2-获取XmlDoment下的节点:

  xmlDocument.GetElementsByTagName("rs:data")

3-获取XmlNode下的属性值:

  node.Attributes["ows_LinkFilename"].Value

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读