2013-12-11 75 views
0

分佈式項目「全球網格大同盟」要得到詳細的用戶信息的能力,例如:如何將XML文檔轉換爲易於使用的C#對象?

<?xml version="1.0" encoding="UTF-8"?> 

<MemberStatsWithTeamHistory> 
    <MemberStats> 
    <MemberStat> 
     <Name>makerofthings7</Name> 
     <MemberId>876300</MemberId> 
     <TeamId>0QGNJ4D832</TeamId> 
     <RegisterDate>2013-12-08</RegisterDate> 
     <LastResult>2013-12-11T11:58:51</LastResult> 
     <NumDevices>4</NumDevices> 
     <StatisticsTotals> 
     <RunTime>1290126</RunTime> 
     <RunTimeRank>415174</RunTimeRank> 
     <Points>61710</Points> 
     <PointsRank>293387</PointsRank> 
     <Results>175</Results> 
     <ResultsRank>282858</ResultsRank> 
     </StatisticsTotals> 
     <StatisticsAverages> 
     <RunTimePerDay>322532</RunTimePerDay> 
     <RunTimePerResult>7372</RunTimePerResult> 
     <PointsPerHourRunTime>172.19713423320002</PointsPerHourRunTime> 
     <PointsPerDay>15427.5</PointsPerDay> 
     <PointsPerResult>352.62857142857104</PointsPerResult> 
     <ResultsPerDay>43.75</ResultsPerDay> 
     </StatisticsAverages> 
     <Resource> 
     <Url>http://www.worldcommunitygrid.org/stat/viewMemberInfo.do?userName=makerofthings7&amp;xml=true</Url> 
     <Description>makerofthings7</Description> 
     </Resource> 
    </MemberStat> 
</MemberStats> 
    <TeamHistory> 
    <Team> 
     <Name>Ripple Labs</Name> 
     <TeamId>0QGNJ4D832       </TeamId> 
     <JoinDate>2013-12-08</JoinDate> 
     <StatisticsTotals> 
     <RunTime>688331</RunTime> 
     <Points>33548</Points> 
     <Results>99</Results> 
     </StatisticsTotals> 
    </Team> 
    </TeamHistory> 
    <MemberStatsByProjects> 
     <Project>   <ProjectName>Mapping Cancer Markers</ProjectName> 
     <ProjectShortName>mcm1</ProjectShortName> 
     <RunTime>518163</RunTime> 
     <Points>22918</Points> 
     <Results>39</Results> 
     </Project>  <Project>   <ProjectName>The Clean Energy Project - Phase 2</ProjectName> 
     <ProjectShortName>cep2</ProjectShortName> 
     <RunTime>61555</RunTime> 
     <Points>3422</Points> 
     <Results>4</Results> 
     </Project>  <Project>   <ProjectName>[email protected]</ProjectName> 
     <ProjectShortName>faah</ProjectShortName> 
     <RunTime>710408</RunTime> 
     <Points>35370</Points> 
     <Results>132</Results> 
     </Project> </MemberStatsByProjects> 
</MemberStatsWithTeamHistory> 

我能夠去這裏得到這個XML:

http://www.worldcommunitygrid.org/verifyMember.do?name=makerofthings7&code=SECRET CODE HERE 

然後提取的祕密代碼from this page,每個人都是唯一的。

既然這是一個很好的原因,我想鼓勵其他人使用這個API並「競爭」治療。

問題

我怎麼可以把這個XML文件,並將其轉換成可以很容易被消耗用戶使用C#類?

+1

嘗試[XmlDocument的(http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(V = vs.110)的.aspx) – ryrich

+0

你有xsd定義?你可以使用http://xsd2code.codeplex.com/,它集成到VS. – Konstantin

回答

0

我,將它從XML到JSON http://www.utilities-online.info/xmltojson/#.Uqi40eIa6So
而從JSON來CSHARP:http://json2csharp.com/
希望幫助:)

public class StatisticsTotals 
{ 
    public string RunTime { get; set; } 
    public string RunTimeRank { get; set; } 
    public string Points { get; set; } 
    public string PointsRank { get; set; } 
    public string Results { get; set; } 
    public string ResultsRank { get; set; } 
} 

public class StatisticsAverages 
{ 
    public string RunTimePerDay { get; set; } 
    public string RunTimePerResult { get; set; } 
    public string PointsPerHourRunTime { get; set; } 
    public string PointsPerDay { get; set; } 
    public string PointsPerResult { get; set; } 
    public string ResultsPerDay { get; set; } 
} 

public class Resource 
{ 
    public string Url { get; set; } 
    public string Description { get; set; } 
} 

public class MemberStat 
{ 
    public string Name { get; set; } 
    public string MemberId { get; set; } 
    public string TeamId { get; set; } 
    public string RegisterDate { get; set; } 
    public string LastResult { get; set; } 
    public string NumDevices { get; set; } 
    public StatisticsTotals StatisticsTotals { get; set; } 
    public StatisticsAverages StatisticsAverages { get; set; } 
    public Resource Resource { get; set; } 
} 

public class MemberStats 
{ 
    public MemberStat MemberStat { get; set; } 
} 

public class StatisticsTotals2 
{ 
    public string RunTime { get; set; } 
    public string Points { get; set; } 
    public string Results { get; set; } 
} 

public class Team 
{ 
    public string Name { get; set; } 
    public string TeamId { get; set; } 
    public string JoinDate { get; set; } 
    public StatisticsTotals2 StatisticsTotals { get; set; } 
} 

public class TeamHistory 
{ 
    public Team Team { get; set; } 
} 

public class Project 
{ 
    public string ProjectName { get; set; } 
    public string ProjectShortName { get; set; } 
    public string RunTime { get; set; } 
    public string Points { get; set; } 
    public string Results { get; set; } 
} 

public class MemberStatsByProjects 
{ 
    public List<Project> Project { get; set; } 
} 

public class MemberStatsWithTeamHistory 
{ 
    public MemberStats MemberStats { get; set; } 
    public TeamHistory TeamHistory { get; set; } 
    public MemberStatsByProjects MemberStatsByProjects { get; set; } 
} 

public class RootObject 
{ 
    public MemberStatsWithTeamHistory MemberStatsWithTeamHistory { get; set; } 
} 
-1

複製XML到剪貼板,去一個C#類文件,請將光標你想要粘貼類的位置,然後轉到編輯 - >選擇性粘貼 - >將XML粘貼爲C#類,就完成了。

+0

呃,沒有。這不存在。 – gunr2171

+0

當然,至少在VS2013 Ultimate – neo112

+0

[鏈接到img顯示其可能](http://i.imgur.com/4eQVX36.png) – neo112

1

您可以使用XmlSerialzier ....

XmlSerializer serializer = new XmlSerializer(typeof(MemberStatsWithTeamHistory)); 
var obj = serializer.Deserialize(File.OpenRead(fname)) as MemberStatsWithTeamHistory; 

PS:這是不一樣的@ AydinAdn的答案。我改變,例如,MemberStatsByProjects定義,使其工作


public class StatisticsTotals 
{ 
    public string RunTime { get; set; } 
    public string RunTimeRank { get; set; } 
    public string Points { get; set; } 
    public string PointsRank { get; set; } 
    public string Results { get; set; } 
    public string ResultsRank { get; set; } 
} 

public class StatisticsAverages 
{ 
    public string RunTimePerDay { get; set; } 
    public string RunTimePerResult { get; set; } 
    public string PointsPerHourRunTime { get; set; } 
    public string PointsPerDay { get; set; } 
    public string PointsPerResult { get; set; } 
    public string ResultsPerDay { get; set; } 
} 

public class Resource 
{ 
    public string Url { get; set; } 
    public string Description { get; set; } 
} 

public class MemberStat 
{ 
    public string Name { get; set; } 
    public string MemberId { get; set; } 
    public string TeamId { get; set; } 
    public string RegisterDate { get; set; } 
    public string LastResult { get; set; } 
    public string NumDevices { get; set; } 
    public StatisticsTotals StatisticsTotals { get; set; } 
    public StatisticsAverages StatisticsAverages { get; set; } 
    public Resource Resource { get; set; } 
} 

public class MemberStats 
{ 
    public MemberStat MemberStat { get; set; } 
} 

public class StatisticsTotals2 
{ 
    public string RunTime { get; set; } 
    public string Points { get; set; } 
    public string Results { get; set; } 
} 

public class Team 
{ 
    public string Name { get; set; } 
    public string TeamId { get; set; } 
    public string JoinDate { get; set; } 
    public StatisticsTotals2 StatisticsTotals { get; set; } 
} 

public class TeamHistory 
{ 
    public Team Team { get; set; } 
} 

public class Project 
{ 
    public string ProjectName { get; set; } 
    public string ProjectShortName { get; set; } 
    public string RunTime { get; set; } 
    public string Points { get; set; } 
    public string Results { get; set; } 
} 

public class MemberStatsWithTeamHistory 
{ 
    public MemberStats MemberStats { get; set; } 
    public TeamHistory TeamHistory { get; set; } 
    public List<Project> MemberStatsByProjects { get; set; } 
} 
0

首先我用XSD.EXE從使用模式XSD的XML創建的類。 (xsd.exe)。這將創建一個類。然後使用泛型:

public class XmlSerializer<T> 
    { 
     /// <summary> 
     /// Load a Xml File and Deserialize into and object   
     /// </summary> 
     /// <param name="xml">Xml String</param> 
     /// <returns>Object representing the xml. You should catch an InvalidCastException</returns> 
     public T DeserializeXmlProductContent(String xml) 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(T)); 

      T obj; 
      using (StringReader reader = new StringReader(xml)) 
      { 
       obj = (T)serializer.Deserialize(reader); 
      } 
      return obj; 


     } 
     /// <summary> 
     /// Serialize an Object to a Xml String 
     /// </summary> 
     /// <param name="obj">Any Object</param> 
     /// <returns>Xml String</returns> 
     public String SerializeProductContentToXml(T obj) 
     { 

      XmlSerializer serializer = new XmlSerializer(obj.GetType()); 

      using (StringWriter writer = new StringWriter()) 
      { 
       serializer.Serialize(writer, obj); 

       return writer.ToString(); 
      } 

     } 
    } 
相關問題