¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

XML in vs2008 #simplsharp


 

I'm working with a device that returns an XML payload upon query.? I have this device working perfectly on 4 series BUT now I need to make this backwards compatible with 3 series (using vs2008 and CF 3.5) because of the processor shortage. I can't figure out what the best method is to manage and then deserialize the XML properly. What I'm doing on 4 series is deserializing using another class I created which doesn't exist in the CF 3.5 sandbox.

Example Code on 4 Series:

using (StreamReader sr = new StreamReader(s)) //"s" is my response stream from a webrequest
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var xmlData = sr.ReadToEnd();? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlData)))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? XmlSerializer serializer = new XmlSerializer(typeof(ExampleDataModel), new XmlRootAttribute("data"));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ExampleDataModel?data = (ExampleDataModel) serializer.Deserialize(ms);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // do other stuff with my data
? ? ? ? ? ? ? ? ? ? ? ? ? ? }

Example of the XML return from my device

<?xml version="1.0" encoding="UTF-8" ?>
<data serial="0x4a">
? ? <ts>1667580367</ts>
? ? <r t="P" n="Name2" did="2">
? ? ? ? <v>9215522798</v>
? ? ? ? <i>0</i>
? ? </r>
? ? <r t="P" n="Name3" did="3">
? ? ? ? <v>6551567803</v>
? ? ? ? <i>0</i>
? ? </r>
? ? <r t="P" n="Name15" did="15">
? ? ? ? <v>-6025876766</v>
? ? ? ? <i>6</i>
? ? </r>
? ? <r t="P" n="Name4" did="4">
? ? ? ? <v>-6372792531</v>
? ? ? ? <i>6</i>
? ? </r>
? ? <r t="P" n="Name5" did="5">
? ? ? ? <v>81964060</v>
? ? ? ? <i>-5</i>
? ? </r>
? ? <r t="P" n="Name6" did="6">
? ? ? ? <v>-664628793</v>
? ? ? ? <i>-9</i>
? ? </r>
? ? <r t="P" n="Name7" did="7">
? ? ? ? <v>-598632391</v>
? ? ? ? <i>-10</i>
? ? </r>
? ? <r t="P" n="Name9" did="9">
? ? ? ? <v>-7313215090</v>
? ? ? ? <i>-87</i>
? ? </r>
? ? <r t="P" n="Name10" did="10">
? ? ? ? <v>-2644948853</v>
? ? ? ? <i>-90</i>
? ? </r>
? ? <r t="P" n="Name11" did="11">
? ? ? ? <v>-1432490</v>
? ? ? ? <i>-3</i>
? ? </r>
? ? <r t="P" n="Name12" did="12">
? ? ? ? <v>-6767026506</v>
? ? ? ? <i>-444</i>
? ? </r>
? ? <r t="P" n="Name13" did="13">
? ? ? ? <v>-4801078066</v>
? ? ? ? <i>-228</i>
? ? </r>
? ? <r t="P" n="Name8" did="8">
? ? ? ? <v>-2331104692</v>
? ? ? ? <i>-104</i>
? ? </r>
? ? <r t="P" n="Name0" did="0">
? ? ? ? <v>40365273148</v>
? ? ? ? <i>747</i>
? ? </r>
? ? <r t="P" n="Name1" did="1">
? ? ? ? <v>36484275778</v>
? ? ? ? <i>569</i>
? ? </r>
</data>

Any help is greatly appreciated as I'm currently stuck.


 

¿ªÔÆÌåÓý

I was able to use XDocument on 3 series

using Crestron.SimplSharp.CrestronXmlLinq;

XDocument xdoc = XDocument.Parse(xmlString);


On Nov 8, 2022, at 1:44 PM, Alex Teague <alext@...> wrote:

I'm working with a device that returns an XML payload upon query.? I have this device working perfectly on 4 series BUT now I need to make this backwards compatible with 3 series (using vs2008 and CF 3.5) because of the processor shortage. I can't figure out what the best method is to manage and then deserialize the XML properly. What I'm doing on 4 series is deserializing using another class I created which doesn't exist in the CF 3.5 sandbox.

Example Code on 4 Series:

using (StreamReader sr = new StreamReader(s)) //"s" is my response stream from a webrequest
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var xmlData = sr.ReadToEnd();? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlData)))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? XmlSerializer serializer = new XmlSerializer(typeof(ExampleDataModel), new XmlRootAttribute("data"));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ExampleDataModel?data = (ExampleDataModel) serializer.Deserialize(ms);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // do other stuff with my data
? ? ? ? ? ? ? ? ? ? ? ? ? ? }

Example of the XML return from my device

<?xml version="1.0" encoding="UTF-8" ?>
<data serial="0x4a">
? ? <ts>1667580367</ts>
? ? <r t="P" n="Name2" did="2">
? ? ? ? <v>9215522798</v>
? ? ? ? <i>0</i>
? ? </r>
? ? <r t="P" n="Name3" did="3">
? ? ? ? <v>6551567803</v>
? ? ? ? <i>0</i>
? ? </r>
? ? <r t="P" n="Name15" did="15">
? ? ? ? <v>-6025876766</v>
? ? ? ? <i>6</i>
? ? </r>
? ? <r t="P" n="Name4" did="4">
? ? ? ? <v>-6372792531</v>
? ? ? ? <i>6</i>
? ? </r>
? ? <r t="P" n="Name5" did="5">
? ? ? ? <v>81964060</v>
? ? ? ? <i>-5</i>
? ? </r>
? ? <r t="P" n="Name6" did="6">
? ? ? ? <v>-664628793</v>
? ? ? ? <i>-9</i>
? ? </r>
? ? <r t="P" n="Name7" did="7">
? ? ? ? <v>-598632391</v>
? ? ? ? <i>-10</i>
? ? </r>
? ? <r t="P" n="Name9" did="9">
? ? ? ? <v>-7313215090</v>
? ? ? ? <i>-87</i>
? ? </r>
? ? <r t="P" n="Name10" did="10">
? ? ? ? <v>-2644948853</v>
? ? ? ? <i>-90</i>
? ? </r>
? ? <r t="P" n="Name11" did="11">
? ? ? ? <v>-1432490</v>
? ? ? ? <i>-3</i>
? ? </r>
? ? <r t="P" n="Name12" did="12">
? ? ? ? <v>-6767026506</v>
? ? ? ? <i>-444</i>
? ? </r>
? ? <r t="P" n="Name13" did="13">
? ? ? ? <v>-4801078066</v>
? ? ? ? <i>-228</i>
? ? </r>
? ? <r t="P" n="Name8" did="8">
? ? ? ? <v>-2331104692</v>
? ? ? ? <i>-104</i>
? ? </r>
? ? <r t="P" n="Name0" did="0">
? ? ? ? <v>40365273148</v>
? ? ? ? <i>747</i>
? ? </r>
? ? <r t="P" n="Name1" did="1">
? ? ? ? <v>36484275778</v>
? ? ? ? <i>569</i>
? ? </r>
</data>

Any help is greatly appreciated as I'm currently stuck.


 

¿ªÔÆÌåÓý

Here is part of the code I wrote for reading xml data from a file for a module I have that tracks the angle and elevation of the sun, compares that with data for each window, and automates the operation of shades based on whether the sun is shining through a window or not.

Hope this helps

??????? private bool Read_XML_Data(string xml_data_file, string path)
??????? {
??????????? try
??????????? {
??????????????? string file_path = String.Format("\\{2}\\{0}\\{1}", InitialParametersClass.ProgramIDTag, xml_data_file, path);

??????????????? //Check if the File Exists
??????????????? if (!Crestron.SimplSharp.CrestronIO.File.Exists(file_path))
??????????????? {
??????????????????? return (false);
??????????????? }

??????????????? //open xml file
??????????????? XDocument doc;
??????????????? FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
??????????????? XmlReader xr = new XmlReader(fs);
??????????????? xr.MoveToContent();
??????????????? doc = XDocument.Load(xr);

??????????????? #region Read General Information
??????????????? XElement generalElement = doc
??????????????????? .Element("ShadeInformation")
??????????????????? .Element("GeneralInformation");

?? ???? ??? ??? if (((string)generalElement.Element("version")) == null)??? ??? //Check if version is embedded in XML file
?? ???? ??? ??? {
?? ???? ??? ??? ??? xml_version = 1;??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? //if no version embedded in xml then it is version 1
?? ???? ??? ??? }
?? ???? ??? ??? else
?? ???? ??? ??? {
?? ???? ??? ??? ??? xml_version = Convert.ToDouble(generalElement.Element("version").Value);
?? ???? ??? ??? }

??????????????? latitude = Convert.ToDouble(generalElement.Element("latitude").Value);

??????????????? longitude = Convert.ToDouble(generalElement.Element("longitude").Value);

??????????????? magnetic_declination_degrees = Convert.ToDouble(generalElement.Element("magnetic_declination_degrees").Value);
??????????????? #endregion

??????????????? #region Read Shade Data
??????????????? //Find root element for data for each shade
??????????????? XElement root_shade = doc
??????????????????? .Element("ShadeInformation")
??????????????????? .Element("Shades");

??????????????? //count the number of shades
??????????????? int s_count = root_shade.Elements().Count();

??????????????? //allocate storage for data
??????????????? shade_data = new shade[s_count];

??????????????? IEnumerable<XElement> shades = root_shade.Elements();

??????????????? int j = 0;??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? //initialize index counter
??????????????? foreach (XElement sh in shades)??? ??? ??? ??? ??? ??? ??? ??? ??? //loop through xml elements
??????????????? {
?? ???? ??? ??? ??? #region Shade Data
?? ???? ??? ??? ??? //create new shade instance
??????????????????? shade_data[j] = new shade();

??????????????????? //read data elements from xml file and add them to the object instance
??????????????????? shade_data[j].name = sh.Element("name").Value;

On 11/8/2022 12:54 PM, Bruce Levy via groups.io wrote:

I was able to use XDocument on 3 series

using Crestron.SimplSharp.CrestronXmlLinq;

XDocument xdoc = XDocument.Parse(xmlString);


On Nov 8, 2022, at 1:44 PM, Alex Teague <alext@...> wrote:

I'm working with a device that returns an XML payload upon query.? I have this device working perfectly on 4 series BUT now I need to make this backwards compatible with 3 series (using vs2008 and CF 3.5) because of the processor shortage. I can't figure out what the best method is to manage and then deserialize the XML properly. What I'm doing on 4 series is deserializing using another class I created which doesn't exist in the CF 3.5 sandbox.

Example Code on 4 Series:

using (StreamReader sr = new StreamReader(s)) //"s" is my response stream from a webrequest
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var xmlData = sr.ReadToEnd();? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlData)))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? XmlSerializer serializer = new XmlSerializer(typeof(ExampleDataModel), new XmlRootAttribute("data"));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ExampleDataModel?data = (ExampleDataModel) serializer.Deserialize(ms);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // do other stuff with my data
? ? ? ? ? ? ? ? ? ? ? ? ? ? }

Example of the XML return from my device

<?xml version="1.0" encoding="UTF-8" ?>
<data serial="0x4a">
? ? <ts>1667580367</ts>
? ? <r t="P" n="Name2" did="2">
? ? ? ? <v>9215522798</v>
? ? ? ? <i>0</i>
? ? </r>
? ? <r t="P" n="Name3" did="3">
? ? ? ? <v>6551567803</v>
? ? ? ? <i>0</i>
? ? </r>
? ? <r t="P" n="Name15" did="15">
? ? ? ? <v>-6025876766</v>
? ? ? ? <i>6</i>
? ? </r>
? ? <r t="P" n="Name4" did="4">
? ? ? ? <v>-6372792531</v>
? ? ? ? <i>6</i>
? ? </r>
? ? <r t="P" n="Name5" did="5">
? ? ? ? <v>81964060</v>
? ? ? ? <i>-5</i>
? ? </r>
? ? <r t="P" n="Name6" did="6">
? ? ? ? <v>-664628793</v>
? ? ? ? <i>-9</i>
? ? </r>
? ? <r t="P" n="Name7" did="7">
? ? ? ? <v>-598632391</v>
? ? ? ? <i>-10</i>
? ? </r>
? ? <r t="P" n="Name9" did="9">
? ? ? ? <v>-7313215090</v>
? ? ? ? <i>-87</i>
? ? </r>
? ? <r t="P" n="Name10" did="10">
? ? ? ? <v>-2644948853</v>
? ? ? ? <i>-90</i>
? ? </r>
? ? <r t="P" n="Name11" did="11">
? ? ? ? <v>-1432490</v>
? ? ? ? <i>-3</i>
? ? </r>
? ? <r t="P" n="Name12" did="12">
? ? ? ? <v>-6767026506</v>
? ? ? ? <i>-444</i>
? ? </r>
? ? <r t="P" n="Name13" did="13">
? ? ? ? <v>-4801078066</v>
? ? ? ? <i>-228</i>
? ? </r>
? ? <r t="P" n="Name8" did="8">
? ? ? ? <v>-2331104692</v>
? ? ? ? <i>-104</i>
? ? </r>
? ? <r t="P" n="Name0" did="0">
? ? ? ? <v>40365273148</v>
? ? ? ? <i>747</i>
? ? </r>
? ? <r t="P" n="Name1" did="1">
? ? ? ? <v>36484275778</v>
? ? ? ? <i>569</i>
? ? </r>
</data>

Any help is greatly appreciated as I'm currently stuck.


 

Thanks guys! I have it working now. I really appreciate the help.