繁体   English   中英

XML-有没有更简单的方法

[英]XML - Is there an easier way

标题没有解释这是什么,但我真的不知道该写些什么。

该代码有效,我只是想知道是否有更好的方法。

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);
XmlNodeList xnod0 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 34]/buy");
XmlNodeList xnod1 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 34]/sell");
XmlNodeList xnod2 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 35]/buy");
XmlNodeList xnod3 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 35]/sell");
XmlNodeList xnod4 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 36]/buy");
XmlNodeList xnod5 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 36]/sell");
XmlNodeList xnod6 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 37]/buy");
XmlNodeList xnod7 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 37]/sell");
XmlNodeList xnod8 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 38]/buy");
XmlNodeList xnod9 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 38]/sell");
XmlNodeList xnod10 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 39]/buy");
XmlNodeList xnod11 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 39]/sell");
XmlNodeList xnod12 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 40]/buy");
XmlNodeList xnod13 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 40]/sell");
XmlNodeList xnod14 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 11399]/buy");
XmlNodeList xnod15 = xdoc.SelectNodes("/evec_api/marketstat/type[@id = 11399]/sell");
foreach (XmlElement xnod in xnod0)
{
    TriPrAmarB.Text = xnod["max"].InnerText;
}
foreach (XmlElement xnode0 in xnod1)
{
    TriPrAmarS.Text = xnode0["max"].InnerText;
}
foreach (XmlElement xnode4 in xnod4)
{
    MexPrAmarB.Text = xnode4["max"].InnerText;
}
foreach (XmlElement xnode5 in xnod5)
{
    MexPrAmarS.Text = xnode5["max"].InnerText;
}
foreach (XmlElement xnode6 in xnod6)
{
    IsoPrAmarB.Text = xnode6["max"].InnerText;
}
foreach (XmlElement xnode7 in xnod7)
{
    IsoPrAmarS.Text = xnode7["max"].InnerText;
}
foreach (XmlElement xnode2 in xnod2)
{
    PyrPrAmarB.Text = xnode2["max"].InnerText;
}
foreach (XmlElement xnode3 in xnod3)
{
    PyrPrAmarS.Text = xnode3["max"].InnerText;
}
foreach (XmlElement xnode8 in xnod8)
{
    NocPrAmarB.Text = xnode8["max"].InnerText;
}
foreach (XmlElement xnode9 in xnod9)
{
    NocPrAmarS.Text = xnode9["max"].InnerText;
}
foreach (XmlElement xnode12 in xnod12)
{
    MegPrAmarB.Text = xnode12["max"].InnerText;
}
foreach (XmlElement xnode13 in xnod13)
{
    MegPrAmarS.Text = xnode13["max"].InnerText;
}

如您所见,即时消息使用API​​链接获取我要“缩减”的XML文件,但它包含多个ID节点,需要将它们分离并从中获取元素值,然后将该值放入文本/标签框中。 目前,我正在这样做(参见代码)。 太乱了,让我压力很大。 有没有更好的办法?? 还是我要解决所有这些错误?

对于那些不想在这里加载Web链接的人:

 <?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>

不,不要那样做。 这太可怕了。 您应该使用.NET中已经存在的序列化组件。

这是一个了不起的例子

<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); 
    } 
}

在我看来,您只需要将代码重构为一些小的辅助方法,并在设置文本框时使用它们即可。 例如,这样的事情将使您可以轻松地基于ID,交易类型(买入或卖出)和统计信息类型(最大,最小,平均等)从xml中获取任何类型的统计信息的值:

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;添加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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM