public static Device FromXML(string xml) { XStream xstream = new XStream(); xstream.Alias("Device", typeof(Device)); xstream.Alias("main", typeof(Main)); xstream.Alias("mains", typeof(Main[])); xstream.Alias("CmdInfo", typeof(CmdInfo)); xstream.Alias("CmdInfos", typeof(CmdInfo[])); try { if (xml != null) { xml = xml.Replace(@"<\/", "</"); Device ddd = (Device)xstream.FromXml(xml);//"<Device><cmdInfos><CmdInfo><name>yearmonth</name> <address>002F</address> <csharpType>System.Int16</csharpType> <unitFactor>0</unitFactor></CmdInfo> <CmdInfo><name>dayhour</name> <address>002E</address> <csharpType>System.Int16</csharpType> <unitFactor>0</unitFactor></CmdInfo> <CmdInfo><name>minutesecond</name> <address>002D</address> <csharpType>System.Int16</csharpType> <unitFactor>0</unitFactor></CmdInfo> <CmdInfo><name>zljyggl</name> <address>006A</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>zssyggl</name> <address>0092</address> <csharpType>System.Int32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>a1</name> <address>00B4</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>a2</name> <address>00B6</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>a3</name> <address>00B8</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>v1</name> <address>00A4</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.1</unitFactor></CmdInfo> <CmdInfo><name>v2</name> <address>00A6</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.1</unitFactor></CmdInfo> <CmdInfo><name>v3</name> <address>00A8</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.1</unitFactor></CmdInfo> <CmdInfo><name>pf</name> <address>00C5</address> <csharpType>System.Int16</csharpType> <unitFactor>0.001</unitFactor></CmdInfo></cmdInfos></Device>"); if (ddd == null) { return new Device(); } else { return ddd; } } else { return new Device(); } } catch (Exception ex) { throw ex; } }
/** * Initialize socket server connections through xml configuration file. */ public static void initialize() { //http://code.google.com/p/xstream-dot-net/ StreamReader reader = File.OpenText("controller.xml"); string xml = reader.ReadToEnd(); reader.Close(); XStream xstream = new XStream(); xstream.Alias<List<SocketConnection>>("connections"); xstream.Alias<SocketConnection>("connection"); connectionList = xstream.FromXml<List<SocketConnection>>(xml); }
public virtual void TestSerializeMapping() { string alias = "_testAlias_"; object value = GetValue(); xstream.Alias(alias, shortType); string xml = xstream.ToXml(value); Assert.IsNotNull(xml); Assert.AreEqual("<" + alias + ">" + value.ToString() + "</" + alias + ">", xml); }
public void should_deserialize_a_generic_list() { string xml = "<TestPeople><People><TestPerson><ID>12</ID><Name>Joe</Name></TestPerson></People></TestPeople>"; XStream xs = new XStream(); xs.Alias <TestPeople35>("TestPeople"); xs.Alias <TestPerson35>("TestPerson"); TestPeople35 value = xs.FromXml <TestPeople35>(xml); Assert.AreEqual(1, value.People.Count); }
/** * Initialize socket server connections through xml configuration file. */ public static void initialize() { //http://code.google.com/p/xstream-dot-net/ StreamReader reader = File.OpenText("controller.xml"); string xml = reader.ReadToEnd(); reader.Close(); XStream xstream = new XStream(); xstream.Alias <List <SocketConnection> >("connections"); xstream.Alias <SocketConnection>("connection"); connectionList = xstream.FromXml <List <SocketConnection> >(xml); }
public void act() { XStream xs = new XStream(); xs.Alias("person", typeof(TestPerson)); string xml = xs.ToXml(person); actual = xs.FromXml <TestPerson>(xml); }
public void act() { XStream xs = new XStream(); xs.Alias <TestPerson>("person"); string xml = xs.ToXml(person); actual = xs.FromXml(xml) as TestPerson; }
public void should_serialize_a_generic_list() { XStream xs = new XStream(); xs.Alias <TestPeople35>("TestPeople"); xs.Alias <TestPerson35>("TestPerson"); TestPeople35 ppl = new TestPeople35(); TestPerson35 person = new TestPerson35(); person.ID = 12; person.Name = "Joe"; ppl.People.Add(person); //ppl.people1 = new[] {person}; string xml = xs.ToXml(ppl); Assert.AreEqual("<TestPeople><People><TestPerson><ID>12</ID><Name>Joe</Name></TestPerson></People></TestPeople>", xml); }
public void ShouldAllowAliasingOfAClass() { var xStream = new XStream(); xStream.Alias<Software>("software"); const string expectedXml = "<software><name>apache</name></software>"; Assert.That(xStream.ToXml(new Software("apache")), Is.EqualTo(expectedXml)); Assert.That(xStream.FromXml(expectedXml), Is.EqualTo(new Software("apache"))); }
public void should_be_able_to_use_a_setter_property() { string xml = "<person><ID>2</ID></person>"; XStream xs = new XStream(); xs.Alias <TestPerson35>("person"); var person = xs.FromXml <TestPerson35>(xml); Assert.AreEqual(2, person.ID); }
public static Plugin Read(string fileName) { XmlDocument doc = new XmlDocument(); try { doc.Load(fileName); } catch (Exception) { InvalidPluginException e = new InvalidPluginException("Error while parsing plugin file: " + fileName); throw e; } XmlNode pluginNode = doc.GetElementsByTagName("Plugin").Item(0); if (pluginNode == null) { InvalidPluginException e = new InvalidPluginException("There is no plugin definition in file: " + fileName); throw e; } XStream xs = new XStream(); xs.Alias("Plugin", typeof(PluginConfig)); PluginConfig pluginXML = null; try { pluginXML = xs.FromXml(pluginNode.OuterXml) as PluginConfig; } catch (Exception) { InvalidPluginException e = new InvalidPluginException("Wrong plugin definition in file: " + fileName); throw e; } try { pluginXML.Valid(); } catch (InvalidPluginException ex) { InvalidPluginException e = new InvalidPluginException(ex.Message + " in file: " + fileName, ex); e.Plugin = pluginXML; throw e; } Plugin plugin = new Plugin(pluginXML); plugin.Xml = pluginNode.OuterXml; return(plugin); }
public void should_be_able_to_get_properties() { int id = 2; XStream xs = new XStream(); xs.Alias <TestPerson35>("person"); var person = new TestPerson35 { ID = id }; string xml = xs.ToXml(person); Assert.IsTrue(xml.Contains("<ID>2</ID>")); }
public static Device FromXML(string xml) { XStream xstream = new XStream(); xstream.Alias("Device", typeof(Device)); xstream.Alias("main", typeof(Main)); xstream.Alias("mains", typeof(Main[])); xstream.Alias("CmdInfo", typeof(CmdInfo)); xstream.Alias("CmdInfos", typeof(CmdInfo[])); try { if (xml != null) { xml = xml.Replace(@"<\/", "</"); Device ddd = (Device)xstream.FromXml(xml);//"<Device><cmdInfos><CmdInfo><name>yearmonth</name> <address>002F</address> <csharpType>System.Int16</csharpType> <unitFactor>0</unitFactor></CmdInfo> <CmdInfo><name>dayhour</name> <address>002E</address> <csharpType>System.Int16</csharpType> <unitFactor>0</unitFactor></CmdInfo> <CmdInfo><name>minutesecond</name> <address>002D</address> <csharpType>System.Int16</csharpType> <unitFactor>0</unitFactor></CmdInfo> <CmdInfo><name>zljyggl</name> <address>006A</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>zssyggl</name> <address>0092</address> <csharpType>System.Int32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>a1</name> <address>00B4</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>a2</name> <address>00B6</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>a3</name> <address>00B8</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.01</unitFactor></CmdInfo> <CmdInfo><name>v1</name> <address>00A4</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.1</unitFactor></CmdInfo> <CmdInfo><name>v2</name> <address>00A6</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.1</unitFactor></CmdInfo> <CmdInfo><name>v3</name> <address>00A8</address> <csharpType>System.UInt32</csharpType> <unitFactor>0.1</unitFactor></CmdInfo> <CmdInfo><name>pf</name> <address>00C5</address> <csharpType>System.Int16</csharpType> <unitFactor>0.001</unitFactor></CmdInfo></cmdInfos></Device>"); if (ddd == null) { return(new Device()); } else { return(ddd); } } else { return(new Device()); } } catch (Exception ex) { throw ex; } }