2016-01-29 67 views
2

我有以下XML示例,我想要讀取數據。我只提供了一個「項目」元素,但是這可以在「項目」根目錄中有許多「項目」。如何讀取XML元素c#(示例代碼提供)

<?xml version="1.0" encoding="utf-8"?> 
<projects> 
    <project> 
    <details> 
     <projectName><![CDATA[CxWtGZxYT]]></projectName> 
     <uniqueID>Pt144</uniqueID> 
     <collaboratingList> 
     <collaboratingOrganisation id="5318" value="EpCyxCv RvGxrXAYXGpA xCxWtGZxYT"/> 
     <collaboratingOrganisation id="0000" value="EpCyxCv RvGxrXAYXGpA xCxWtGZxYTd"/> 
     </collaboratingList> 
     <researchOutputList> 
     <item> 
      <pubDate>2014-02-04T00:00:00+00:00</pubDate> 
      <title><![CDATA[rGDEZ]]></title> 
      <link>link</link> 
      <guid>guid</guid> 
      <description><![CDATA[uGDB BDstA rGDEZ]]></description> 
     </item> 
     <item> 
      <pubDate>2015-08-04T00:00:00+00:00</pubDate> 
      <title><![CDATA[AERx CApCYZ.]]></title> 
      <link>link</link> 
      <guid>guid</guid> 
      <description><![CDATA[vwtpY]]></description> 
     </item> 
     </researchOutputList> 
    </details> 
    </project> 
</projects> 

我可以讀取<projectName><uniqueId>但不能<researchOutputList><collaboratingList>。我不知道我缺少什麼或者我的XML結構是否正確。我在下面的代碼:

[Serializable] 
    [XmlRoot("projects")] 
    public class BulkProjectRoot 
    { 
     public BulkProjectRoot() 
     { 
      BulkProjectDetails = new List<BulkProjectData>(); 
     } 

     [XmlArray("project")] 
     [XmlArrayItem("details")] 
     public List<BulkProjectData> BulkProjectDetails { get; set; } 
    } 

[Serializable] 
    [XmlRoot("details")] 

    public class BulkProjectData 
    { 
     public BulkProjectData() 
     { 
      ProjectResearches = new List<ProjectResearchOutputs>(); 
     } 

     [XmlElement("projectName")] 
     public string Name { get; set; } 

     [XmlElement("uniqueID")] 
     public string ProjectUniqueId { get; set; } 

     [XmlElement("researchOutputList")] 
     public List<ProjectResearchOutputs> ProjectResearches { get; set; } 

    } 

[Serializable] 
[XmlRoot("researchOutputList")] 
public class ProjectResearchOutputs 
{ 
    public ProjectResearchOutputs() 
    { 
     ResearchItemsList = new List<ResearchOutputItems>(); 
    } 

    [XmlElement("item")] 
    public List<ResearchOutputItems> ResearchItemsList { get; set; } 

} 

    [Serializable] 
    [XmlRoot("item")] 

    public class ResearchOutputItems 
    { 

     [XmlElement("pubDate")] 
     public DateTime? PublishDate { get; set; } 

     [XmlElement("title")] 
     public string ResearchTitle { get; set; } 

     [XmlElement("link")] 
     public string ResearchLink { get; set; } 

     [XmlElement("guid")] 
     public string ResearchGuid { get; set; } 

     [XmlElement("description")] 
     public string ResearchDescription { get; set; } 

    } 

回答

1

的reasearchlist精解串用Linqpad,但你缺少你BulkProjectData類的協作信息。

void Main() 
{ 
    var xml = @"<?xml version=""1.0"" encoding=""utf - 8""?> 
    <projects> 
    <project> 
     <details> 
     <projectName><![CDATA[CxWtGZxYT]]></projectName> 
     <uniqueID> Pt144 </uniqueID> 
     <collaboratingList> 
      <collaboratingOrganisation id = ""5318"" value = ""EpCyxCv RvGxrXAYXGpA xCxWtGZxYT"" /> 
       <collaboratingOrganisation id = ""0000"" value = ""EpCyxCv RvGxrXAYXGpA xCxWtGZxYTd"" /> 
       </collaboratingList> 
       <researchOutputList> 
       <item> 
        <pubDate>2014-02-04T00:00:00+00:00</pubDate> 
          <title><![CDATA[rGDEZ]]></title> 
          <link> link </link> 
          <guid> guid </guid> 
          <description><![CDATA[uGDB BDstA rGDEZ]]></description> 
          </item> 
          <item> 
          <pubDate>2015-08-04T00:00:00+00:00</pubDate> 
             <title><![CDATA[AERx CApCYZ.]]></title> 
             <link> link </link> 
             <guid> guid </guid> 
             <description><![CDATA[vwtpY]]></description> 
            </item> 
            </researchOutputList> 
           </details> 
           </project> 
          </projects>"; 

      var serializer = new XmlSerializer(typeof(BulkProjectRoot));   
      var result = (BulkProjectRoot)serializer.Deserialize(new StringReader(xml));    

      result.Dump(); 

enter image description here

+0

我的代碼,但無法將其粘貼在這裏遺憾。感謝您的答覆。 – JADE

1

僅僅指剛做出這種改變 -

[Serializable] 
[XmlRoot("details")] 

public class BulkProjectData 
{ 
    public BulkProjectData() 
    { 
     // Not a list 

     ProjectResearches = new ProjectResearchOutputs(); 
    } 

    [XmlElement("projectName")] 
    public string Name { get; set; } 

    [XmlElement("uniqueID")] 
    public string ProjectUniqueId { get; set; } 

    [XmlElement("researchOutputList")] 
    public ProjectResearchOutputs ProjectResearches { get; set; } 
} 
+0

Amit Kumar Ghosh,\t 我已經改變了這一行,但仍然沒有返回researchoutputlist。列表計數爲0. – JADE

+0

這也是正確的,我只需要實現ICloneable來獲取數據。 – JADE