Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.66 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <interlocking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3. <signals>
  4. <signal ref="SignalRef_1">
  5. <aspectSpeedDependencies>
  6. <aspectSpeedDependency aspect="REF" vApproach="VApproach" vPass="Vpas">
  7. <targetRef ref="TargetRef" />
  8. <targetRef ref="TargetRef" />
  9. <targetRef ref="TargetRef" />
  10. </aspectSpeedDependency>
  11. <aspectSpeedDependency aspect="REF" vApproach="VApproach" vPass="Vpas">
  12. <targetRef ref="TargetRef" />
  13. <targetRef ref="TargetRef" />
  14. <targetRef ref="TargetRef" />
  15. </aspectSpeedDependency>
  16. <aspectSpeedDependency aspect="REF" vApproach="VApproach" vPass="Vpas">
  17. <targetRef ref="TargetRef" />
  18. <targetRef ref="TargetRef" />
  19. <targetRef ref="TargetRef" />
  20. </aspectSpeedDependency>
  21. </aspectSpeedDependencies>
  22. </signal>
  23. <signal ref="SignalRef_1">
  24. <aspectSpeedDependencies>
  25. <aspectSpeedDependency aspect="REF" vApproach="VApproach" vPass="Vpas">
  26. <targetRef ref="TargetRef" />
  27. <targetRef ref="TargetRef" />
  28. <targetRef ref="TargetRef" />
  29. </aspectSpeedDependency>
  30. <aspectSpeedDependency aspect="REF" vApproach="VApproach" vPass="Vpas">
  31. <targetRef ref="TargetRef" />
  32. <targetRef ref="TargetRef" />
  33. <targetRef ref="TargetRef" />
  34. </aspectSpeedDependency>
  35. <aspectSpeedDependency aspect="REF" vApproach="VApproach" vPass="Vpas">
  36. <targetRef ref="TargetRef" />
  37. <targetRef ref="TargetRef" />
  38. <targetRef ref="TargetRef" />
  39. </aspectSpeedDependency>
  40. </aspectSpeedDependencies>
  41. </signal>
  42. </signals>
  43. <routes>
  44. <route id="1">
  45. <start>
  46. <signalRef ref="pro.Routes.Route.Start.SignalRef.Ref" />
  47. </start>
  48. <target>
  49. <signalRef ref="Target.SingalRef" />
  50. </target>
  51. <elements>
  52. <switchRef>
  53. <switch ref="Ref" course="Left" />
  54. </switchRef>
  55. <levelcrossingRef>
  56. <levelcrossing ref="Ref" beam="Beam" />
  57. <levelcrossing ref="Ref" beam="Beam" />
  58. <levelcrossing ref="Ref" beam="Beam" />
  59. </levelcrossingRef>
  60. <trainDetectorRef>
  61. <trackCircuitBorder ref="Ref" />
  62. <trackCircuitBorder ref="Ref" />
  63. <trackCircuitBorder ref="Ref" />
  64. </trainDetectorRef>
  65. </elements>
  66. <flankElements>Flank</flankElements>
  67. <routePriority rank="1" />
  68. </route>
  69. </routes>
  70. </interlocking>
  71.  
  72. using System;
  73. using System.Collections.Generic;
  74. using System.Diagnostics;
  75. using System.IO;
  76. using System.Linq;
  77. using System.Text;
  78. using System.Threading.Tasks;
  79. using System.Windows;
  80. using System.Windows.Controls;
  81. using System.Windows.Data;
  82. using System.Windows.Documents;
  83. using System.Windows.Input;
  84. using System.Windows.Media;
  85. using System.Windows.Media.Imaging;
  86. using System.Windows.Shapes;
  87. using System.Xml;
  88. using System.Xml.Serialization;
  89. //using CoreElements.Core.Interlocking;
  90. using System.Xml.Linq;
  91. using System.Reflection;
  92.  
  93. namespace TreeviewTest.Stck
  94. {
  95. /// <summary>
  96. /// Interaction logic for Window1.xaml
  97. /// </summary>
  98. public partial class Window1 : Window
  99. {
  100. public Window1()
  101. {
  102. InitializeComponent();
  103. }
  104.  
  105.  
  106. private void ExecutedLoadXML(object sender, ExecutedRoutedEventArgs e)
  107. {
  108.  
  109. string executableLocation = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  110. string xslLocation = System.IO.Path.Combine(executableLocation, "Interlocking.xml");
  111.  
  112. XDocument xmlData = XDocument.Load(xslLocation, LoadOptions.None);
  113. var Interlocking = XmlSerializationHelper.LoadFromXML<Interlocking>(xmlData.ToString());
  114.  
  115. var children = new List<Interlocking>();
  116. children.Add(Interlocking);
  117.  
  118. treeView1.ItemsSource = null;
  119. treeView1.Items.Clear();
  120. treeView1.ItemsSource = children;
  121. }
  122.  
  123. private void ExecutedSaveXML(object sender, ExecutedRoutedEventArgs e)
  124. {
  125. var planList = treeView1.ItemsSource as IList<Interlocking>;
  126. if (planList != null && planList.Count > 0)
  127. {
  128. // Kludge to force pending edits to update
  129. treeView1.Focus();
  130. // Replace with actual save code!
  131. Debug.WriteLine(planList[0].GetXml());
  132. }
  133. }
  134. }
  135.  
  136. public static class CustomCommands
  137. {
  138. public static readonly RoutedUICommand LoadXMLCommand = new RoutedUICommand("Load XML", "LoadXML", typeof(Window1));
  139.  
  140. public static readonly RoutedUICommand SaveXMLCommand = new RoutedUICommand("Save XML", "SaveXML", typeof(Window1));
  141. }
  142.  
  143. public static class XmlSerializationHelper
  144. {
  145. public static string GetXml<T>(T obj, XmlSerializer serializer, bool omitStandardNamespaces)
  146. {
  147. using (var textWriter = new StringWriter())
  148. {
  149. XmlWriterSettings settings = new XmlWriterSettings();
  150. settings.Indent = true; // For cosmetic purposes.
  151. settings.IndentChars = " "; // For cosmetic purposes.
  152. using (var xmlWriter = XmlWriter.Create(textWriter, settings))
  153. {
  154. if (omitStandardNamespaces)
  155. {
  156. XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  157. ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
  158. serializer.Serialize(xmlWriter, obj, ns);
  159. }
  160. else
  161. {
  162. serializer.Serialize(xmlWriter, obj);
  163. }
  164. }
  165. return textWriter.ToString();
  166. }
  167. }
  168.  
  169. public static string GetXml<T>(this T obj, bool omitNamespace)
  170. {
  171. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  172. return GetXml(obj, serializer, omitNamespace);
  173. }
  174.  
  175. public static string GetXml<T>(this T obj)
  176. {
  177. return GetXml(obj, false);
  178. }
  179.  
  180. public static T LoadFromXML<T>(this string xmlString)
  181. {
  182. return xmlString.LoadFromXML<T>(new XmlSerializer(typeof(T)));
  183. }
  184.  
  185. public static T LoadFromXML<T>(this string xmlString, XmlSerializer serial)
  186. {
  187. T returnValue = default(T);
  188.  
  189. using (StringReader reader = new StringReader(xmlString))
  190. {
  191. object result = serial.Deserialize(reader);
  192. if (result is T)
  193. {
  194. returnValue = (T)result;
  195. }
  196. }
  197. return returnValue;
  198. }
  199.  
  200. public static T LoadFromFile<T>(string filename)
  201. {
  202. XmlSerializer serial = new XmlSerializer(typeof(T));
  203. try
  204. {
  205. using (var fs = new FileStream(filename, FileMode.Open))
  206. {
  207. object result = serial.Deserialize(fs);
  208. if (result is T)
  209. {
  210. return (T)result;
  211. }
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. Debug.WriteLine(ex.ToString());
  217. throw;
  218. }
  219. return default(T);
  220. }
  221. }
  222. }
  223.  
  224. <Window x:Class="Test_Thesis.MainWindow"
  225. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  226. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  227. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  228. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  229. xmlns:o="clr-namespace:Test_Thesis"
  230. mc:Ignorable="d"
  231. Title="Window1" Height="300" Width="600">
  232. <Window.CommandBindings>
  233. <CommandBinding Command="o:CustomCommands.LoadXMLCommand" Executed="ExecutedLoadXML"/>
  234. <CommandBinding Command="o:CustomCommands.SaveXMLCommand" Executed="ExecutedSaveXML"/>
  235. </Window.CommandBindings>
  236. <Window.Resources>
  237. <HierarchicalDataTemplate DataType="{x:Type o:Interlocking}" ItemsSource="{Binding Path=Signals}">
  238. <TextBlock Text="Interlocking">
  239. </TextBlock>
  240. </HierarchicalDataTemplate >
  241. <HierarchicalDataTemplate DataType="{x:Type o:Signals}" ItemsSource="{Binding Path=Signal}">
  242. <TextBlock Text="Signal">
  243. </TextBlock>
  244. </HierarchicalDataTemplate >
  245.  
  246. <!--<HierarchicalDataTemplate DataType="{x:Type o:Signal}" ItemsSource="{Binding Path=AspectSpeedDependencies}">
  247. <TextBlock Text="Signals">
  248. </TextBlock>
  249. </HierarchicalDataTemplate >
  250. <HierarchicalDataTemplate DataType="{x:Type o:AspectSpeedDependencies}" ItemsSource="{Binding Path=AspectSpeedDependency}">
  251. <TextBlock Text="AspectSpeedDependencies">
  252. </TextBlock>
  253. </HierarchicalDataTemplate >-->
  254.  
  255. <HierarchicalDataTemplate DataType="{x:Type o:Signal}" ItemsSource="{Binding Path=AspectSpeedDependencies}">
  256. <Grid Margin="3" MinWidth="300">
  257. <Grid.RowDefinitions>
  258. <RowDefinition />
  259. </Grid.RowDefinitions>
  260. <Grid.ColumnDefinitions>
  261. <ColumnDefinition />
  262. <ColumnDefinition />
  263. </Grid.ColumnDefinitions>
  264. <TextBlock Text="Ref" Grid.Column="0" Grid.Row="0"/>
  265. <TextBox Text="{Binding Path=Ref, Mode=TwoWay}" Grid.Column="1" Grid.Row="0"/>
  266. </Grid>
  267. </HierarchicalDataTemplate >
  268. <HierarchicalDataTemplate DataType="{x:Type o:AspectSpeedDependencies}" ItemsSource="{Binding Path=AspectSpeedDependency}">
  269. <TextBlock Text="AspectSpeedDependencies">
  270. </TextBlock>
  271. </HierarchicalDataTemplate >
  272.  
  273. <HierarchicalDataTemplate DataType="{x:Type o:AspectSpeedDependency}" ItemsSource="{Binding Path=TargetRef}">
  274.  
  275. <Border BorderBrush="Gray" BorderThickness="1" MinWidth="300">
  276. <StackPanel Height="auto" Width="auto">
  277. <TextBlock Text="AspectSpeedDependency:" />
  278.  
  279. <StackPanel Orientation="Horizontal">
  280.  
  281. <TextBlock Text="Aspect:" Margin="1"/>
  282. <TextBox Text="{Binding Path=Aspect, Mode=TwoWay}" Margin="1"/>
  283. <TextBlock Text="VApproach:" Margin="1"/>
  284. <TextBox Text="{Binding Path=VApproach, Mode=TwoWay}" Margin="1"/>
  285. <TextBlock Text="VPass:" Margin="1"/>
  286. <TextBox Text="{Binding Path=VPass, Mode=TwoWay}" Margin="1"/>
  287. </StackPanel>
  288.  
  289.  
  290.  
  291. <!--<TextBlock Text="Aspect:" Grid.Column="0" Grid.Row="2"/>
  292. <TextBox Text="{Binding Path=Aspect, Mode=TwoWay}" Grid.Column="1" Grid.Row="2"/>
  293. <TextBlock Text="VApproach:" Grid.Column="0" Grid.Row="3"/>
  294. <TextBox Text="{Binding Path=VApproach, Mode=TwoWay}" Grid.Column="1" Grid.Row="3"/>
  295. <TextBlock Text="VPass:" Grid.Column="0" Grid.Row="4"/>
  296. <TextBox Text="{Binding Path=VPass, Mode=TwoWay}" Grid.Column="1" Grid.Row="4"/>-->
  297.  
  298.  
  299.  
  300. </StackPanel>
  301.  
  302. </Border>
  303. </HierarchicalDataTemplate >
  304.  
  305. <DataTemplate DataType="{x:Type o:TargetRef}">
  306. <Border BorderBrush="Brown" BorderThickness="1" MinWidth="300">
  307. <Grid Margin="3" >
  308. <Grid.RowDefinitions>
  309. <RowDefinition />
  310. <RowDefinition />
  311. <RowDefinition />
  312. <RowDefinition />
  313. </Grid.RowDefinitions>
  314. <Grid.ColumnDefinitions>
  315. <ColumnDefinition />
  316. <ColumnDefinition />
  317. </Grid.ColumnDefinitions>
  318. <TextBlock Text="Ref:" Grid.Column="0" Grid.Row="0"/>
  319. <TextBox Text="{Binding Path=Ref, Mode=TwoWay}" Grid.Column="1" Grid.Row="0"/>
  320.  
  321.  
  322.  
  323. </Grid>
  324. </Border>
  325. </DataTemplate >
  326. <!-- if we remove below element it will show first leg of xml structure-->
  327. <HierarchicalDataTemplate DataType="{x:Type o:Interlocking}" ItemsSource="{Binding Path=Routes}">
  328. <TextBlock Text="Routes">
  329. </TextBlock>
  330. </HierarchicalDataTemplate >
  331. </Window.Resources>
  332. <DockPanel>
  333. <ToolBarTray DockPanel.Dock="Top">
  334. <ToolBar>
  335. <Button Command="o:CustomCommands.LoadXMLCommand" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
  336. <Button Command="o:CustomCommands.SaveXMLCommand" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
  337. </ToolBar>
  338. </ToolBarTray>
  339. <Grid DockPanel.Dock="Bottom">
  340. <TreeView Margin="3" Name="treeView1">
  341. <TreeView.ItemContainerStyle>
  342. <Style TargetType="{x:Type TreeViewItem}">
  343. <Setter Property="IsExpanded" Value="True" />
  344. </Style>
  345. </TreeView.ItemContainerStyle>
  346. </TreeView>
  347. </Grid>
  348. </DockPanel>
  349. </Window>
  350.  
  351. using System;
  352. using System.Collections.Generic;
  353. using System.Linq;
  354. using System.Text;
  355. using System.Threading.Tasks;
  356. using System.Xml.Serialization;
  357.  
  358.  
  359. namespace TreeviewTest.Stck
  360. {
  361.  
  362.  
  363.  
  364. [XmlRoot(ElementName = "targetRef")]
  365. public class TargetRef
  366. {
  367. [XmlAttribute(AttributeName = "ref")]
  368. public string Ref { get; set; }
  369. }
  370.  
  371. [XmlRoot(ElementName = "aspectSpeedDependency")]
  372. public class AspectSpeedDependency
  373. {
  374. [XmlElement(ElementName = "targetRef")]
  375. public List<TargetRef> TargetRef { get; set; }
  376. [XmlAttribute(AttributeName = "aspect")]
  377. public string Aspect { get; set; }
  378. [XmlAttribute(AttributeName = "vApproach")]
  379. public string VApproach { get; set; }
  380. [XmlAttribute(AttributeName = "vPass")]
  381. public string VPass { get; set; }
  382. }
  383.  
  384. [XmlRoot(ElementName = "aspectSpeedDependencies")]
  385. public class AspectSpeedDependencies
  386. {
  387. [XmlElement(ElementName = "aspectSpeedDependency")]
  388. public List<AspectSpeedDependency> AspectSpeedDependency { get; set; }
  389. }
  390.  
  391. [XmlRoot(ElementName = "signal")]
  392. public class Signal
  393. {
  394. [XmlElement(ElementName = "aspectSpeedDependencies")]
  395. public List<AspectSpeedDependencies> AspectSpeedDependencies { get; set; }
  396. [XmlAttribute(AttributeName = "ref")]
  397. public string Ref { get; set; }
  398. }
  399.  
  400. [XmlRoot(ElementName = "signals")]
  401. public class Signals
  402. {
  403. [XmlElement(ElementName = "signal")]
  404. public List<Signal> Signal { get; set; }
  405. }
  406.  
  407. [XmlRoot(ElementName = "signalRef")]
  408. public class SignalRef
  409. {
  410. [XmlAttribute(AttributeName = "ref")]
  411. public string Ref { get; set; }
  412. }
  413.  
  414. [XmlRoot(ElementName = "start")]
  415. public class Start
  416. {
  417. [XmlElement(ElementName = "signalRef")]
  418. public List<SignalRef> SignalRef { get; set; }
  419. }
  420.  
  421. [XmlRoot(ElementName = "target")]
  422. public class Target
  423. {
  424. [XmlElement(ElementName = "signalRef")]
  425. public List<SignalRef> SignalRef { get; set; }
  426. }
  427.  
  428. [XmlRoot(ElementName = "switch")]
  429. public class Switch
  430. {
  431. [XmlAttribute(AttributeName = "ref")]
  432. public string Ref { get; set; }
  433. [XmlAttribute(AttributeName = "course")]
  434. public string Course { get; set; }
  435. }
  436.  
  437. [XmlRoot(ElementName = "switchRef")]
  438. public class SwitchRef
  439. {
  440. [XmlElement(ElementName = "switch")]
  441. public List<Switch> Switch { get; set; }
  442. }
  443.  
  444. [XmlRoot(ElementName = "levelcrossing")]
  445. public class Levelcrossing
  446. {
  447. [XmlAttribute(AttributeName = "ref")]
  448. public string Ref { get; set; }
  449. [XmlAttribute(AttributeName = "beam")]
  450. public string Beam { get; set; }
  451. }
  452.  
  453. [XmlRoot(ElementName = "levelcrossingRef")]
  454. public class LevelcrossingRef
  455. {
  456. [XmlElement(ElementName = "levelcrossing")]
  457. public List<Levelcrossing> Levelcrossing { get; set; }
  458. }
  459.  
  460. [XmlRoot(ElementName = "trackCircuitBorder")]
  461. public class TrackCircuitBorder
  462. {
  463. [XmlAttribute(AttributeName = "ref")]
  464. public string Ref { get; set; }
  465. }
  466.  
  467. [XmlRoot(ElementName = "trainDetectorRef")]
  468. public class TrainDetectorRef
  469. {
  470. [XmlElement(ElementName = "trackCircuitBorder")]
  471. public List<TrackCircuitBorder> TrackCircuitBorder { get; set; }
  472. }
  473.  
  474. [XmlRoot(ElementName = "elements")]
  475. public class Elements
  476. {
  477. [XmlElement(ElementName = "switchRef")]
  478. public List<SwitchRef> SwitchRef { get; set; }
  479. [XmlElement(ElementName = "levelcrossingRef")]
  480. public List<LevelcrossingRef> LevelcrossingRef { get; set; }
  481. [XmlElement(ElementName = "trainDetectorRef")]
  482. public List<TrainDetectorRef> TrainDetectorRef { get; set; }
  483.  
  484. [XmlIgnore]
  485. public IList ElementChildren
  486. {
  487. get
  488. {
  489. return new CompositeCollection()
  490. {
  491. new CollectionContainer() { Collection = SwitchRef },
  492. new CollectionContainer() { Collection = LevelcrossingRef },
  493. new CollectionContainer() { Collection = TrainDetectorRef }
  494. };
  495. }
  496. }
  497. }
  498.  
  499. [XmlRoot(ElementName = "routePriority")]
  500. public class RoutePriority
  501. {
  502. [XmlAttribute(AttributeName = "rank")]
  503. public string Rank { get; set; }
  504. }
  505.  
  506. [XmlRoot(ElementName = "route")]
  507. public class Route
  508. {
  509. [XmlElement(ElementName = "start")]
  510. public List<Start> Start { get; set; }
  511. [XmlElement(ElementName = "target")]
  512. public List<Target> Target { get; set; }
  513. [XmlElement(ElementName = "elements")]
  514. public List<Elements> Elements { get; set; }
  515. [XmlElement(ElementName = "flankElements")]
  516. public string FlankElements { get; set; }
  517. [XmlElement(ElementName = "routePriority")]
  518. public List<RoutePriority> RoutePriority { get; set; }
  519. [XmlAttribute(AttributeName = "id")]
  520. public string Id { get; set; }
  521.  
  522. [XmlIgnore]
  523. public IList RouteChildern
  524. {
  525. get
  526. {
  527. return new CompositeCollection()
  528. {
  529. new CollectionContainer() { Collection = Start },
  530. new CollectionContainer() { Collection = Target },
  531. new CollectionContainer() { Collection = Elements },
  532.  
  533. new CollectionContainer() { Collection = RoutePriority }
  534.  
  535. };
  536. }
  537. }
  538. }
  539.  
  540.  
  541. [XmlRoot(ElementName = "routes")]
  542. public class Routes
  543. {
  544. [XmlElement(ElementName = "route")]
  545. public List<Route> Route { get; set; }
  546. }
  547.  
  548. [XmlRoot(ElementName = "interlocking")]
  549. public class Interlocking
  550. {
  551. [XmlElement(ElementName = "signals")]
  552. public List<Signals> Signals { get; set; }
  553. [XmlElement(ElementName = "routes")]
  554. public List<Routes> Routes { get; set; }
  555. [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
  556. public string Xsi { get; set; }
  557. [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
  558. public string Xsd { get; set; }
  559.  
  560. [XmlIgnore]
  561. public IList InterlockingChildren
  562. {
  563. get
  564. {
  565. return new CompositeCollection()
  566. {
  567. new CollectionContainer() { Collection = Signals },
  568. new CollectionContainer() { Collection = Routes }
  569. };
  570. }
  571. }
  572. }
  573. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement