Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML—有更简单的方法吗_C#_Asp.net_Xml - Fatal编程技术网

C# XML—有更简单的方法吗

C# XML—有更简单的方法吗,c#,asp.net,xml,C#,Asp.net,Xml,标题不能解释这是什么,但我真的不知道该写什么 我只是想知道是否有更好的方法 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&t

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

我只是想知道是否有更好的方法

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;
}
正如您所看到的,im使用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>

-     
-    
-    
-   
3457195520
5.44
5.83
4.18
0.61
5.51
5.82
-    
3243443787
6.47
15.82
5.87
1.80
6.20
6.05
-    
6800639307
5.86
15.82
0.20
1.87
5.82
3.60
-    
-
616253967
9.98
10.70
6.34
1.24
10.48
10.70
-    
657965395
14.21
24
11.44
6.01
11.68
11.44
-    
1274219362
12.17
24
6.34
6.14
11.44
7.82
-
-    
322291783
53.35
56
38.26
4.23
55.25
55.99
-
580953174
63.29
89.40
59
7.81
60.44
59.60
-    
903244957
59.74
89.40
38.26
8.48
59.82
43.37
-
-    
147215461
92.36
96.68
38.40
13.23
96.64
96.67
-    
213867507
114.33
280
100
40.85
102.29
100.93
-    
371082968
102.82
280
10.64
40.74
100.97
47.42
-
-    
37059521
438.61
456.73
355
32.35
452.53
456.73
-    
41961165
536.59
650
469
54.90
513.63
469
-    
79020686
490.64
650
355
68.43
469
379.86
-    
-
17527906
1011.91
1267
751
163.64
991.85
1266.25
-    
11084558
1472.06
1899
1428.98
87.97
1470.38
1428.98
-    
31612464
1101.04
1899
251.01
259.73
1182.01
251.01
-
-
11218433
991.80
1110.04
876.01
83.77
969.94
1110.03
-    
13324678
1298.68
1842
1199.97
140.89
1228.95
1205.26
-    
24543111
1158.41
1842
876.01
175.90
1206
888.68
-
-    
383592
9206.37
11045.43
5510.66
1680.48
9100.05
11016.97
-    
420217
11902.63
17183.99
11150.99
1956.07
11488
11150.99
-
803809
10615.93
17183.99
5510.66
2169.53
11150.99
5513.25

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

这是一个令人惊奇的故事


4.
罗希尼
德里
公共类地址详细信息
{ 
[XmlElement(“数字”)]
public int HouseNo{get;set;}
[XmlElement(“街道”)]
公共字符串StreetName{get;set;}
[XmlElement(“CityName”)]
}
公共静态void Main(字符串[]args)
{ 
AddressDetails details=新的AddressDetails();
详情:编号=4;
details.StreeName=“Rohini”;
详细信息。城市=“德里”;
序列化(细节);
}   
静态公共void序列化(AddressDetails)
{ 
XmlSerializer serializer=新的XmlSerializer(typeof(AddressDetails));
使用(TextWriter=newstreamWriter(@“C:\Xml.Xml”))
{
序列化器。序列化(编写器,详细信息);
} 
}

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

这是一个令人惊奇的故事


4.
罗希尼
德里
公共类地址详细信息
{ 
[XmlElement(“数字”)]
public int HouseNo{get;set;}
[XmlElement(“街道”)]
公共字符串StreetName{get;set;}
[XmlElement(“CityName”)]
}
公共静态void Main(字符串[]args)
{ 
AddressDetails details=新的AddressDetails();
详情:编号=4;
details.StreeName=“Rohini”;
详细信息。城市=“德里”;
序列化(细节);
}   
静态公共void序列化(AddressDetails)
{ 
XmlSerializer serializer=新的XmlSerializer(typeof(AddressDetails));
使用(TextWriter=newstreamWriter(@“C:\Xml.Xml”))
{
序列化器。序列化(编写器,详细信息);
} 
}

在我看来,您只需要将代码重构为一些小的辅助方法,并在设置文本框时使用这些方法。例如,类似这样的内容将允许您根据id、交易类型(买入或卖出)和统计类型(最大、最小、平均等)从xml轻松获取任何类型的统计值:


下面是一个简短的演示(控制台应用程序):

在我看来,您只需要将代码重构为几个小的帮助程序方法,并在设置文本框时使用这些方法。例如,类似这样的东西可以让您根据id、交易类型(buy或buy)从xml中轻松获取任何类型的stat的值
<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); 
    } 
}
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();
}
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);