Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How to deal with XML in netduino plus 2 framework 4.2

XML Netduino Plus 2 NETMF 4.2

  • Please log in to reply
1 reply to this topic

#1 javadch

javadch

    New Member

  • Members
  • Pip
  • 2 posts

Posted 23 August 2013 - 03:07 PM

I am trying to read from an XML file, which is stored on the SD card of a netduino plus 2, using NETFM 4.2. There is no compile time error but I get Not Supported Error on the call to XmlReader.Create(Stream) method!?

 

I am trying to do something similar to http://stackoverflow...xmlreader-fails but with 4.2 framework and plus 2 hardware.

 

Any help is welcome.

 

 



#2 Anthony Glenwright

Anthony Glenwright

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts

Posted 27 August 2013 - 05:53 AM

The netduino's don't have the XML runtimes, because they take a lot of memory.

 

I wrote a (fairly crappy) xml reader.  There are probably many things wrong with it, but it works for my purposes, maybe it will work for you.

namespace Data{	public class XmlReader	{		private string mstrInput;		private string mstrName;		private int mintCurrentPosition;		private System.Collections.Stack mobjStack = new System.Collections.Stack();		public XmlReader(string Input)		{			mstrInput = Input;			mintCurrentPosition = 0;		}		public bool Read()		{			// find the next <element> 			int intElementSpace = 0;			int intElementNameEnd = 0;			bool blnElementFound = false;			while (!(blnElementFound || mintCurrentPosition >= mstrInput.Length)) {				switch (mstrInput.Substring(mintCurrentPosition, 1)) {					case "<":						// element start (or end)						if (mstrInput.Substring(mintCurrentPosition, 2) == "</") {							// element end							mstrName = mobjStack.Peek().ToString();							if (mstrInput.Substring(mintCurrentPosition, mstrName.Length + 3) == "</" + mstrName + ">") {								// long-form element close, pop the current stack item								mobjStack.Pop();							}							mintCurrentPosition = mintCurrentPosition + (mstrName.Length + 3);						} else {							// element start							if (mstrInput.Substring(mintCurrentPosition, 2) == "<?") {								// xml or other declaration, skip								intElementNameEnd = mstrInput.Substring(mintCurrentPosition).IndexOf(">");								mintCurrentPosition = mintCurrentPosition + intElementNameEnd;							} else {								intElementNameEnd = mstrInput.Substring(mintCurrentPosition).IndexOf(">");								intElementSpace = mstrInput.Substring(mintCurrentPosition).IndexOf(" ");								if (intElementSpace > 0 && intElementSpace < intElementNameEnd) {									mstrName = mstrInput.Substring(mintCurrentPosition + 1, intElementSpace - 1);									mintCurrentPosition = mintCurrentPosition + intElementSpace + 1;								} else {									mstrName = mstrInput.Substring(mintCurrentPosition + 1, intElementNameEnd - 1);									mintCurrentPosition = mintCurrentPosition + intElementNameEnd + 1;								}								mobjStack.Push(mstrName);								blnElementFound = true;							}						}						break;					case "/":						// short-form element close						if (mstrInput.Substring(mintCurrentPosition, 2) == "/>") {							//  pop the current stack item							mobjStack.Pop();							mstrName = mobjStack.Peek().ToString();							mintCurrentPosition = mintCurrentPosition + 2;						} else {							mintCurrentPosition = mintCurrentPosition + 1;						}						break;					default:						mintCurrentPosition = mintCurrentPosition + 1;						break;				}			}			return !(mobjStack.Count == 0) & !(mintCurrentPosition >= mstrInput.Length);		}		public string Name {			get { return mstrName; }		}		public string GetAttribute(string Name)		{			string strDataLine = null;			int intElementEnd = 0;			string[] strAttributes = null;			string[] strSplit = null;			string strName = null;			string strValue = null;			intElementEnd = mstrInput.Substring(mintCurrentPosition).IndexOfAny(new char[] {				'>',				'<',				'/'			});			strDataLine = mstrInput.Substring(mintCurrentPosition, intElementEnd);			strAttributes = strDataLine.Split(' ');			foreach (string strAttribute in strAttributes) {				if (strAttribute != string.Empty) {					strSplit = strAttribute.Split('=');					if (strSplit.Length == 2) {						strName = strSplit[0];						strValue = strSplit[1];						strValue = XMLSerializer.Unescape(strValue.Substring(1, strValue.Length - 2));						if (strName == Name) {							return strValue;						}					}				}			}			return "";		}		public Collections.DictionaryEntry[] GetAttributes()		{			string strDataLine = null;			int intElementEnd = 0;			string[] strAttributes = null;			string[] strSplit = null;			string strName = null;			string strValue = null;			System.Collections.ArrayList objAttributes = new System.Collections.ArrayList();			intElementEnd = mstrInput.Substring(mintCurrentPosition).IndexOfAny(new char[] {				'>',				'<',				'/'			});			strDataLine = mstrInput.Substring(mintCurrentPosition, intElementEnd);			strAttributes = strDataLine.Split(' ');			foreach (string strAttribute in strAttributes) {				if (strAttribute != string.Empty) {					strSplit = strAttribute.Split('=');					if (strSplit.Length == 2) {						strName = strSplit[0];						strValue = strSplit[1];						// remove quotes						strValue = XMLSerializer.Unescape(strValue.Substring(1, strValue.Length - 2));						objAttributes.Add(new System.Collections.DictionaryEntry(strName, strValue));					}				}			}			return (Collections.DictionaryEntry[])objAttributes.ToArray(typeof(Collections.DictionaryEntry));		}	}}






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.