Skip to content

Commit

Permalink
Merge pull request #3 from ClutchplateDude/Bugfixes
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
ClutchplateDude authored Jan 12, 2017
2 parents 8ad345e + b2c6dc5 commit 1d6114c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,61 @@ Simple Xml parser for .NET Micro Framework (made for Netduino)

I noticed that the .NET Microframework that's used on Netduinos does not support System.Xml, but I wanted to use Xml in my Netduino projects, so I wrote this simple parser.
It's very barebones (code size and memory usage are always a concern on the Micro Framework). It supports elements and attributes. NO namespaces or more esoteric features.

## Usage
It is quite simple to use this little library:
1. Create an XDoc instance by calling the XDoc.Parse static function, giving it the Xml string.
1. Use the Element() function to get the root node.
1. Use a combination of Element(), Elements() and Attribute() functions to get at the data.

##Caveats
- Attribute values may not contain spaces (parser is somewhat simplistic, but could probably be fixed to support this).
- Only Elements and Attributes are supported.

##Example

Given the following Xml:
```xml
<House>
<Rooms>
<Room Name="LivingRoom" Floor="1" />
<Room Name="MasterBedroom" Floor="2" />
<Room Name="Kitchen" Floor="1">     
<Furniture
<Stove Type="Gas" Burners="4" /> 
<Fridge Brand="GE" />     
</Furniture>
</Room>
</Rooms>
</House>
````
This could be parse by code lide this:
```Csharp
XDoc dom = XDoc.Parse(xmlInputString);
XElem houseElem = dom.Element("House");

foreach (XElem roomElem in houseElem.Element("Rooms").Elements("Room"))
{
string roomName = roomElem.Attribute("Name").Value;
int roomFloor = int.Parse(roomElem.Attribute("Floor").Value);
// ... create a room data structure...
XElem furniture = roomElem.Element("Furniture");
if (furniture != null)
{
foreach (XElem furnitureElem in furniture.Children)
{
var furniturePiece = ReadFurniture(furnitureElem);
// .. do something with the piece...
}
}
}

private static object ReadFurniture(XElem furniture)
{
switch (furniture.Name)
{
case "Stove": return ReadStove(furniture);
case "Fridge": return ReadFridge(furniture);
}
}
````
18 changes: 12 additions & 6 deletions XElem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ internal void AddChild(XElem node)

public XElem Element(string elemName)
{
foreach (XElem elem in this.Children)
if (this.HasChildren)
{
if (elem.Name.Equals(elemName))
foreach (XElem elem in this.Children)
{
return elem;
if (elem.Name.Equals(elemName))
{
return elem;
}
}
}

Expand All @@ -43,11 +46,14 @@ public XElem Element(string elemName)
public XElemList Elements(string elemName)
{
var result = new XElemList();
foreach (XElem elem in this.Children)
if (this.HasChildren)
{
if (elem.Name.Equals(elemName))
foreach (XElem elem in this.Children)
{
result.Add(elem);
if (elem.Name.Equals(elemName))
{
result.Add(elem);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions XmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ namespace MicroFramework.Xml
public class XmlParser
{
private string inputString;
private int currPos = 0;

public XmlParser(string input)
{
this.inputString = input;
this.currPos = 0;
}

public void ReadElement(ref XElem root)
{
int currPos = 0;
int posSpace = 0;
int posNameEnd = 0;
XElem node;

while (currPos >= this.inputString.Length)
while (currPos < this.inputString.Length)
{
switch (this.inputString[currPos])
{
Expand Down

0 comments on commit 1d6114c

Please sign in to comment.