Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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>
Posted
Comments
Sergey Alexandrovich Kryukov 12-Aug-14 17:51pm    
This is very easy, but what do you mean by this "topmost value"? It's clear what is the top element; this is document root. But there is no one single "value". The element has one or more attributes, and it can have several children, and some of those children can be text nodes.

Also, "could only contain subsections or elements, not both" makes no sense at all. There are elements, but there are no "sub-section". Maybe, it's just not properly phrased.

—SA
PIEBALDconsult 12-Aug-14 17:59pm    
Learn XPath.

http://www.w3schools.com/xpath/xpath_intro.asp

1 solution

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 using System.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:
C#
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[^].

[EDIT]

This approach is the fastest possible one, first of all, because you will read only small top portion of a stream.

[END EDIT]

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900