怎麼將XML字元串轉換為XmlDocument,並獲取部分節點值??? 總結關鍵知識點: 1-如何將XML字元串轉換為XmlDocdument: XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xmlSt... ...
我們的目標:解析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|SINOOCEANLAND\\v-zhangqiang vti_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-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|SINOOCEANLAND\\v-zhangqiang vti_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-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|SINOOCEANLAND\\v-zhangqiang vti_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-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|SINOOCEANLAND\\v-zhangqiang vti_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-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|SINOOCEANLAND\\v-zhangqiang vti_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-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>
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'>"; xmlStr += "<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|SINOOCEANLAND\\v-zhangqiang vti_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-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' />"; xmlStr += "<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|SINOOCEANLAND\\v-zhangqiang vti_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-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' />"; xmlStr += "<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|SINOOCEANLAND\\v-zhangqiang vti_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-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' />"; xmlStr += "<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|SINOOCEANLAND\\v-zhangqiang vti_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-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' />"; xmlStr += "<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|SINOOCEANLAND\\v-zhangqiang vti_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-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' />"; xmlStr += "</rs:data>"; xmlStr += "</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