如何阅读Xml中元素的最高价值? [英] How Do I Read The Topmost Value Of Element In The Xml ?
问题描述
我有一个XMl文件,其中最顶层的元素MFM Compatible有一个值。我需要在c#代码中读取这个值并将其存储在变量中。我怎样才能实现它?
I have an XMl file in which the topmost element MFM Compatible has a value. I need to read this value in c# code and store it in a variable. How can i achieve it?
<?xml versin="1.0"?>
<!--Note: each section/subsection has to have a globally unique name-->
<!--Note: each section could only contain subsections or elements, not both-->
-<MFM compatible="a29d.fe3e" version="1.1" xsi:noNamespaceSchemaLocation="/MFM/MFM.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<lane_specific>
-<lane_1>
<alias>4</alias>
<signal>1</signal>
<signal_config>0</signal_config>
<min_yellow_in_10th_sec>25</min_yellow_in_10th_sec>
<red_grace_in_10th_sec>1</red_grace_in_10th_sec>
<red_speed_threshold>13</red_speed_threshold>
<speed_adj_factor_in_128th>128</speed_adj_factor_in_128th>
<stopbar_detection>0</stopbar_detection>
<advanced_option>0</advanced_option>
<RL_1st_photo_flash_spread>8</RL_1st_photo_flash_spread>
<RL_time_to_2nd_photo_in_10th_sec>0</RL_time_to_2nd_photo_in_10th_sec>
<RL_dist_to_2nd_photo>30</RL_dist_to_2nd_photo>
<RL_2nd_photo_flash_spread>8</RL_2nd_photo_flash_spread>
<lane_far_edge_to_datum>27</lane_far_edge_to_datum>
<enforcement_direction>43</enforcement_direction>
<stopbar_mid_lane_distance>74</stopbar_mid_lane_distance>
<first_detect_line_relative>0</first_detect_line_relative>
<second_detect_line_relative>0</second_detect_line_relative>
<third_detect_line_relative>0</third_detect_line_relative>
<speed_enforcement_position>1</speed_enforcement_position>
</lane_1>
-<lane_2>
<alias>3</alias>
<signal>1</signal>
<signal_config>0</signal_config>
<min_yellow_in_10th_sec>25</min_yellow_in_10th_sec>
<red_grace_in_10th_sec>1</red_grace_in_10th_sec>
<red_speed_threshold>13</red_speed_threshold>
<speed_adj_factor_in_128th>128</speed_adj_factor_in_128th>
<stopbar_detection>0</stopbar_detection>
<advanced_option>0</advanced_option>
<RL_1st_photo_flash_spread>8</RL_1st_photo_flash_spread>
<RL_time_to_2nd_photo_in_10th_sec>0</RL_time_to_2nd_photo_in_10th_sec>
<RL_dist_to_2nd_photo>35</RL_dist_to_2nd_photo>
<RL_2nd_photo_flash_spread>8</RL_2nd_photo_flash_spread>
<lane_far_edge_to_datum>38</lane_far_edge_to_datum>
<enforcement_direction>43</enforcement_direction>
<stopbar_mid_lane_distance>73</stopbar_mid_lane_distance>
<first_detect_line_relative>0</first_detect_line_relative>
<second_detect_line_relative>0</second_detect_line_relative>
<third_detect_line_relative>0</third_detect_line_relative>
<speed_enforcement_position>1</speed_enforcement_position>
</lane_2>
-<lane_3>
<alias>2</alias>
<signal>1</signal>
<signal_config>0</signal_config>
<min_yellow_in_10th_sec>25</min_yellow_in_10th_sec>
<red_grace_in_10th_sec>1</red_grace_in_10th_sec>
<red_speed_threshold>13</red_speed_threshold>
<speed_adj_factor_in_128th>128</speed_adj_factor_in_128th>
<stopbar_detection>0</stopbar_detection>
<advanced_option>0</advanced_option>
<RL_1st_photo_flash_spread>8</RL_1st_photo_flash_spread>
<RL_time_to_2nd_photo_in_10th_sec>0</RL_time_to_2nd_photo_in_10th_sec>
<RL_dist_to_2nd_photo>35</RL_dist_to_2nd_photo>
<RL_2nd_photo_flash_spread>8</RL_2nd_photo_flash_spread>
<lane_far_edge_to_datum>51</lane_far_edge_to_datum>
<enforcement_direction>43</enforcement_direction>
<stopbar_mid_lane_distance>73</stopbar_mid_lane_distance>
<first_detect_line_relative>0</first_detect_line_relative>
<second_detect_line_relative>0</second_detect_line_relative>
<third_detect_line_relative>0</third_detect_line_relative>
<speed_enforcement_position>1</speed_enforcement_position>
</lane_3>
-<lane_4>
<alias>1</alias>
<signal>1</signal>
<signal_config>0</signal_config>
<min_yellow_in_10th_sec>25</min_yellow_in_10th_sec>
<red_grace_in_10th_sec>1</red_grace_in_10th_sec>
<red_speed_threshold>13</red_speed_threshold>
<speed_adj_factor_in_128th>128</speed_adj_factor_in_128th>
<stopbar_detection>0</stopbar_detection>
<advanced_option>0</advanced_option>
<RL_1st_photo_flash_spread>8</RL_1st_photo_flash_spread>
<RL_time_to_2nd_photo_in_10th_sec>0</RL_time_to_2nd_photo_in_10th_sec>
<RL_dist_to_2nd_photo>30</RL_dist_to_2nd_photo>
<RL_2nd_photo_flash_spread>8</RL_2nd_photo_flash_spread>
<lane_far_edge_to_datum>65</lane_far_edge_to_datum>
<enforcement_direction>43</enforcement_direction>
<stopbar_mid_lane_distance>72</stopbar_mid_lane_distance>
<first_detect_line_relative>0</first_detect_line_relative>
<second_detect_line_relative>0</second_detect_line_relative>
<third_detect_line_relative>0</third_detect_line_relative>
<speed_enforcement_position>1</speed_enforcement_position>
</lane_4>
</lane_specific>
-<global>
<main_lane_signal>1</main_lane_signal>
<unit>0</unit>
<violation_per_phase>128</violation_per_phase>
<flash1_binary>55</flash1_binary>
<flash2_binary>55</flash2_binary>
<flash3_binary>55</flash3_binary>
<operation_mode>1</operation_mode>
<vehicle_detector>7</vehicle_detector>
<car_speed_threshold>12</car_speed_threshold>
<truck_speed_threshold>12</truck_speed_threshold>
<speed_flash_spread_1st>8</speed_flash_spread_1st>
<speed_time_2nd_shot>1</speed_time_2nd_shot>
<speed_dist_2nd_shot>10</speed_dist_2nd_shot>
<speed_flash_spread_2nd>8</speed_flash_spread_2nd>
<atr_lane_number>9</atr_lane_number>
<site_max_plausible_speed>0</site_max_plausible_speed>
<site_implausible_speed_action>0</site_implausible_speed_action>
<vehicle_length>99</vehicle_length>
<RL_high_speed_threshold>0</RL_high_speed_threshold>
<agile_radar_type>2</agile_radar_type>
</global>
</MFM>
推荐答案
Please see my comment to the question. The problem is not correctly formulated: there is a top element (root), but there is no one single \"top value\". You need to explain it. But, don’t rush to clarify: you can easily sort it out by yourself. For such a simple problem, I would recommend usingSystem.Xml.XmlReader
:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110%29.aspx[^].
Don’t be confused: this class is abstract, with a hidden constructor, so you need to create an instance, in your case, from the file name, using a static factory method: http://msdn.microsoft.com/en-us/library/w8k674bf(v=vs.110).aspx[^].
Basically, if you need just one value, it will be something like this:
Please see my comment to the question. The problem is not correctly formulated: there is a top element (root), but there is no one single "top value". You need to explain it. But, don't rush to clarify: you can easily sort it out by yourself. For such a simple problem, I would recommend usingSystem.Xml.XmlReader
:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110%29.aspx[^].
Don't be confused: this class is abstract, with a hidden constructor, so you need to create an instance, in your case, from the file name, using a static factory method: http://msdn.microsoft.com/en-us/library/w8k674bf(v=vs.110).aspx[^].
Basically, if you need just one value, it will be something like this:
using System.Xml;
// ...
string fileName = // ...
//...
using (XmlReader reader = XmlReader(fileName)) {
while(reader.Read())
if (FindOutWhatYouNeed(
reader.NodeType,
reader.HasValue,
reader.ValueType,
reader.Value)) // whatever you need to determine
// that the data you need is found
{
// use the value of the node here
break;
}
} // reader.Dispose() is automatically called here
I wrote the call to the method FindOutWhatYouNeed
just for example, to illustrate the idea: in your simple case, you just wait until the node is the one you expected, using the current reader
position through the properties I’ve shown in the sample of the call; you have to write this method yourself, using your criteria which I don’t know.
Please see: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read%28v=vs.110%29.aspx[^].
This approach is the fastest possible one, first of all, because you will read only small top portion of a stream.
[END EDIT]
I wrote the call to the method FindOutWhatYouNeed
just for example, to illustrate the idea: in your simple case, you just wait until the node is the one you expected, using the current reader
position through the properties I've shown in the sample of the call; you have to write this method yourself, using your criteria which I don't know.
Please see: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read%28v=vs.110%29.aspx[^].
This approach is the fastest possible one, first of all, because you will read only small top portion of a stream.
[END EDIT]
这篇关于如何阅读Xml中元素的最高价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!