怎么将XML字符串转换为XmlDocument,并获取部分节点值
2017-05-18 11:27
531 查看
我们的目标:解析XML字符串,并获取节点:z:row下的属性:ows_LinkFilename的值集合???
XML字符串数据结构如下:
C#代码如下:
结果图如下:
总结关键知识点:
1-如何将XML字符串转换为XmlDocdument:
XmlDocumentxmlDocument=newXmlDocument();
xmlDocument.LoadXml(xmlStr);
2-获取XmlDoment下的节点:
xmlDocument.GetElementsByTagName("rs:data")
3-获取XmlNode下的属性值:
node.Attributes["ows_LinkFilename"].Value
XML字符串数据结构如下:
<listitemsxmlns: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:dataItemCount='5'> <z:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='6;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:SW|路平_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='6;#2014-03-3109: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-3109: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:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='7;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:EW|_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='7;#2014-03-3109: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-3109: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:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-BZ.003_录用管理标准.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='8;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:SW|路平_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='8;#2014-03-3109: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-3109: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:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-GF.001_内部应聘规范.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='9;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:EW|_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='9;#2014-03-3109: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-3109: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:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-GF.002_内部推荐规范.docx'ows_Modified='2014-03-3109:54:05'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='10;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:EW|_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='10;#2014-03-3109: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-3109: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>
C#代码如下:
stringxmlStr="<listitemsxmlns: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:dataItemCount='5'>"; xmlStr+="<z:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-BZ.001_岗位说明书编制标准.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='6;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:SW|路平_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='6;#2014-03-3109: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-3109: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'/>"; xmlStr+="<z:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-BZ.002_招聘方案设置和发布标准.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='7;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:EW|_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='7;#2014-03-3109: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-3109: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'/>"; xmlStr+="<z:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-BZ.003_录用管理标准.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='8;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:SW|路平_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='8;#2014-03-3109: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-3109: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'/>"; xmlStr+="<z:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-GF.001_内部应聘规范.docx'ows_Modified='2014-03-3109:54:04'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='9;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:EW|_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='9;#2014-03-3109: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-3109: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'/>"; xmlStr+="<z:rowows_DocIcon='docx'ows_LinkFilename='RL-RYTG-V1.0-GF.002_内部推荐规范.docx'ows_Modified='2014-03-3109:54:05'ows_Editor='7437;#(已离职)A-张强'ows_MetaInfo='10;#Subject:SW|远洋地产控股有限公司流程Keywords:EW|_Status:EW|vti_parserversion:SR|12.0.0.6421vti_modifiedby:SR|SINOOCEANLAND\\v-zhangqiangvti_title:EW|_Author:EW|_Category:EW|ContentType:EW|ContentTypeId:SW|0x0101006FC9BC54865EB7418119055CF0BD51C8_Comments:EW|vti_author:SR|SINOOCEANLAND\\v-zhangqiang'ows__ModerationStatus='0'ows__Level='1'ows_Last_x0020_Modified='10;#2014-03-3109: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-3109: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'/>"; xmlStr+="</rs:data>"; xmlStr+="</listitems>"; XmlDocumentxmlDocument=newXmlDocument(); xmlDocument.LoadXml(xmlStr); XmlNodeListlistitemsNodeList=xmlDocument.GetElementsByTagName("rs:data"); XmlNodeListdataNodeList=listitemsNodeList[0].ChildNodes; List<string>result=newList<string>(); foreach(XmlNodenodeindataNodeList) { stringfileName=node.Attributes["ows_LinkFilename"].Value; result.Add(fileName); } result.ForEach(re=> { Console.WriteLine(re); }); Console.ReadLine();
结果图如下:
总结关键知识点:
1-如何将XML字符串转换为XmlDocdument:
XmlDocumentxmlDocument=newXmlDocument();
xmlDocument.LoadXml(xmlStr);
2-获取XmlDoment下的节点:
xmlDocument.GetElementsByTagName("rs:data")
3-获取XmlNode下的属性值:
node.Attributes["ows_LinkFilename"].Value
相关文章推荐
- XML字符串转换成XML Document对象,并生成在指定路径
- 关于XML字符串和XML Document之间的转换
- qt XML中的HTML节点,转换为html格式的字符串
- c++ builder 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- XML字符串和XML DOCUMENT的相互转换
- XML字符串与XML Document 相互转换
- 关于XML字符串和XML Document之间的转换
- (转)XML字符串和XML DOCUMENT的相互转换
- C#中用XmlDocument对象获取XML文件中的节点值
- XmlDocument根据节点的属性值获取节点
- 黄聪:Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 关于XML字符串和XML Document之间的转换
- Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 关于XML字符串和XML Document之间的转换《转》
- XStream:Object与Xml字符串 互相转换 多个同名子节点以及节点属性的处理。
- c++ builder 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 关于XML字符串和XML Document之间的转换
- 设置节点属性;setInterval和setTimeout;转换为字符串;获取字符位置方法;检测字符串长度
- 关于XML字符串和XML Document之间的转换
- 如何把一个json格式的字符串转换成json,并获取他的节点值