XML - 有一种更简单的方法

时间:2016-02-19 14:44:25

标签: c# asp.net xml

标题不解释这是什么,但我真的不知道该放什么;

代码工作我只是想知道是否有更好的方法。

 <?xml version="1.0" encoding="UTF-8"?>    
    -<evec_api method="marketstat_xml" version="2.0">     
    -<marketstat>    
    -<type id="34">    
    -<buy>   
    <volume>3457195520</volume>    
    <avg>5.44</avg>    
    <max>5.83</max>    
    <min>4.18</min>    
    <stddev>0.61</stddev>    
    <median>5.51</median>    
    <percentile>5.82</percentile>    
    </buy>   
    -<sell>    
    <volume>3243443787</volume>    
    <avg>6.47</avg>    
    <max>15.82</max>    
    <min>5.87</min>
    <stddev>1.80</stddev>    
    <median>6.20</median>    
    <percentile>6.05</percentile>    
    </sell>   
    -<all>    
    <volume>6800639307</volume>    
    <avg>5.86</avg>    
    <max>15.82</max>    
    <min>0.20</min>    
    <stddev>1.87</stddev>    
    <median>5.82</median>    
    <percentile>3.60</percentile>    
    </all>
    </type>        
    -<type id="35">    
    -<buy>
     <volume>616253967</volume>    
    <avg>9.98</avg>    
    <max>10.70</max>
    <min>6.34</min>    
    <stddev>1.24</stddev>    
    <median>10.48</median>    
    <percentile>10.70</percentile>    
    </buy>   
    -<sell>    
    <volume>657965395</volume>    
    <avg>14.21</avg>    
    <max>24.00</max>    
    <min>11.44</min>    
    <stddev>6.01</stddev>    
    <median>11.68</median>    
    <percentile>11.44</percentile>    
    </sell> 
    -<all>    
    <volume>1274219362</volume>    
    <avg>12.17</avg>    
    <max>24.00</max>    
    <min>6.34</min>    
    <stddev>6.14</stddev>    
    <median>11.44</median>    
    <percentile>7.82</percentile>    
    </all>    
    </type>
   -<type id="36">
    -<buy>    
    <volume>322291783</volume>    
    <avg>53.35</avg>    
    <max>56.00</max>    
    <min>38.26</min>    
    <stddev>4.23</stddev>    
    <median>55.25</median>    
    <percentile>55.99</percentile>    
    </buy>  
    -<sell>
     <volume>580953174</volume>    
    <avg>63.29</avg>    
    <max>89.40</max>
    <min>59.00</min>    
    <stddev>7.81</stddev>    
    <median>60.44</median>    
    <percentile>59.60</percentile>    
    </sell>
    -<all>    
    <volume>903244957</volume>    
    <avg>59.74</avg>    
    <max>89.40</max>    
    <min>38.26</min>    
    <stddev>8.48</stddev>    
    <median>59.82</median>    
    <percentile>43.37</percentile>    
    </all>
    </type>
    -<type id="37">
    -<buy>    
    <volume>147215461</volume>    
    <avg>92.36</avg>    
    <max>96.68</max>
    <min>38.40</min>    
    <stddev>13.23</stddev>    
    <median>96.64</median>    
    <percentile>96.67</percentile>    
    </buy>
    -<sell>    
    <volume>213867507</volume>    
    <avg>114.33</avg>
    <max>280.00</max>    
    <min>100.00</min>    
    <stddev>40.85</stddev>    
    <median>102.29</median>    
    <percentile>100.93</percentile>    
    </sell>
    -<all>    
    <volume>371082968</volume>    
    <avg>102.82</avg>
    <max>280.00</max>    
    <min>10.64</min>    
    <stddev>40.74</stddev>    
    <median>100.97</median>    
    <percentile>47.42</percentile>    
    </all>    
    </type>  
    -<type id="38">
    -<buy>    
    <volume>37059521</volume>    
    <avg>438.61</avg>
    <max>456.73</max>    
    <min>355.00</min>    
    <stddev>32.35</stddev>    
    <median>452.53</median>    
    <percentile>456.73</percentile>    
    </buy>
    -<sell>    
    <volume>41961165</volume>    
    <avg>536.59</avg>    
    <max>650.00</max>    
    <min>469.00</min>    
    <stddev>54.90</stddev>    
    <median>513.63</median>    
    <percentile>469.00</percentile>    
    </sell>
    -<all>    
    <volume>79020686</volume>    
    <avg>490.64</avg>    
    <max>650.00</max>    
    <min>355.00</min>    
    <stddev>68.43</stddev>    
    <median>469.00</median>    
    <percentile>379.86</percentile>    
    </all>    
    </type>
    -<type id="39">    
        -<buy>

    <volume>17527906</volume>    
    <avg>1011.91</avg>
    <max>1267.00</max>    
    <min>751.00</min>    
    <stddev>163.64</stddev>    
    <median>991.85</median>    
    <percentile>1266.25</percentile>    
    </buy>
    -<sell>    
    <volume>11084558</volume>    
    <avg>1472.06</avg>    
    <max>1899.00</max>    
    <min>1428.98</min>
    <stddev>87.97</stddev>    
    <median>1470.38</median>    
    <percentile>1428.98</percentile>    
    </sell>
    -<all>    
    <volume>31612464</volume>    
    <avg>1101.04</avg>    
    <max>1899.00</max>    
    <min>251.01</min>    
    <stddev>259.73</stddev>    
    <median>1182.01</median>
    <percentile>251.01</percentile>    
    </all>   
    </type> 
    -<type id="40">
    -<buy>
     <volume>11218433</volume>   
    <avg>991.80</avg>    
    <max>1110.04</max>    
    <min>876.01</min>    
    <stddev>83.77</stddev>    
    <median>969.94</median>    
    <percentile>1110.03</percentile>    
    </buy>
    -<sell>    
    <volume>13324678</volume>    
    <avg>1298.68</avg>    
    <max>1842.00</max>    
    <min>1199.97</min>    
    <stddev>140.89</stddev>    
    <median>1228.95</median>    
    <percentile>1205.26</percentile>    
    </sell>        
    -<all>    
    <volume>24543111</volume>    
    <avg>1158.41</avg>    
    <max>1842.00</max>    
    <min>876.01</min>    
    <stddev>175.90</stddev>    
    <median>1206.00</median>    
    <percentile>888.68</percentile>    
    </all>
    </type>
    -<type id="11399">
    -<buy>    
    <volume>383592</volume>    
    <avg>9206.37</avg>    
    <max>11045.43</max>    
    <min>5510.66</min>    
    <stddev>1680.48</stddev>   
    <median>9100.05</median>    
    <percentile>11016.97</percentile>    
    </buy>   
    -<sell>    
    <volume>420217</volume>    
    <avg>11902.63</avg>    
    <max>17183.99</max>    
    <min>11150.99</min>    
    <stddev>1956.07</stddev>    
    <median>11488.00</median>    
    <percentile>11150.99</percentile>    
    </sell>  
    -<all>
     <volume>803809</volume>
        <avg>10615.93</avg>    
    <max>17183.99</max>    
    <min>5510.66</min>
     <stddev>2169.53</stddev>    
    <median>11150.99</median>    
    <percentile>5513.25</percentile>    
    </all>    
    </type>
    </marketstat>    
    </evec_api>

正如您所看到的那样,我使用API​​链接来获取我想要减少的XML文件&#34;但它拥有多个ID节点,我需要分离并从中获取元素值并将该值放入文本/标签框中。目前我这样做(见代码)。它很乱,让我很紧张。有没有更好的办法??或者我说这一切都错了?

对于那些不想在此处加载网络链接的人来说,这是:

{{1}}

2 个答案:

答案 0 :(得分:2)

不,不要这样做。这太可怕了。 您应该使用.NET中已有的Serialization组件。

这是一个了不起的example

<AddressDetails>
    <HouseNo>4</HouseNo>
    <StreetName>Rohini</StreetName>
    <City>Delhi</City>
</AddressDetails>

public class AddressDetails
{ 
    [XmlElement("Number")]
    public int HouseNo { get; set; }
    [XmlElement("Street")] 
    public  string StreetName { get; set; } 
    [XmlElement("CityName")]
}

public static void Main(string[] args) 
{ 
    AddressDetails details = new AddressDetails();
    details.HouseNo = 4;
    details.StreeName = "Rohini";
    details.City = "Delhi";
    Serialize(details);
}   
static public void Serialize(AddressDetails details)
{ 
    XmlSerializer serializer = new XmlSerializer(typeof(AddressDetails)); 
    using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
    {
        serializer.Serialize(writer, details); 
    } 
}

答案 1 :(得分:1)

在我看来,您只需要将代码重构为一些小帮助方法,并在设置文本框时使用它们。例如,这样的东西可以让你根据id,交易类型(买入或卖出)和统计类型(max,min,avg等)从xml轻松获得任何类型的stat值:

enum TranType { Buy, Sell, All };
enum StatType { Volume, Avg, Max, Min, StdDev, Median, Percentile };

private static string GetStat(XmlDocument xdoc, int id, TranType tranType, StatType statType)
{
    string xpath = string.Format("/evec_api/marketstat/type[@id = {0}]/{1}/{2}", 
                   id, tranType.ToString().ToLower(), statType.ToString().ToLower());

    return GetFirstElementText(xdoc, xpath);
}

private static string GetFirstElementText(XmlDocument xdoc, string xpath)
{
    // Get the InnerText of the first XmlElement matching the xpath, if any (otherwise null)
    return xdoc.SelectNodes(xpath).Cast<XmlElement>().Select(x => x.InnerText).FirstOrDefault();
}

注意:您需要在代码顶部添加using System.Linq;(如果您还没有),以便这些辅助方法能够正常工作。

使用这些辅助方法,您可以设置如下文本框:

string OrePrice = "http://api.eve-central.com/api/marketstat?typeid=34&minQ=1&typeid=35&minQ=1&typeid=36&minQ=1&typeid=37&minQ=1&typeid=38&minQ=1&typeid=39&minQ=1&typeid=40&minQ=1&typeid=11399&minQ=1&usesystem=30002187";

XmlDocument xdoc = new XmlDocument();
xdoc.Load(OrePrice);

TriPrAmarB.Text = GetStat(xdoc, 34, TranType.Buy, StatType.Max);
TriPrAmarS.Text = GetStat(xdoc, 34, TranType.Sell, StatType.Max);
PyrPrAmarB.Text = GetStat(xdoc, 35, TranType.Buy, StatType.Max);
PyrPrAmarS.Text = GetStat(xdoc, 35, TranType.Sell, StatType.Max);
MexPrAmarB.Text = GetStat(xdoc, 36, TranType.Buy, StatType.Max);
MexPrAmarS.Text = GetStat(xdoc, 36, TranType.Sell, StatType.Max);
IsoPrAmarB.Text = GetStat(xdoc, 37, TranType.Buy, StatType.Max);
IsoPrAmarS.Text = GetStat(xdoc, 37, TranType.Sell, StatType.Max);
NocPrAmarB.Text = GetStat(xdoc, 38, TranType.Buy, StatType.Max);
NocPrAmarS.Text = GetStat(xdoc, 38, TranType.Sell, StatType.Max);
MegPrAmarB.Text = GetStat(xdoc, 40, TranType.Buy, StatType.Max);
MegPrAmarS.Text = GetStat(xdoc, 40, TranType.Sell, StatType.Max);

这是一个简短的演示(控制台应用):https://dotnetfiddle.net/RlUCEq