snippetcsharpMinor
Parsing XML to create IList
Viewed 0 times
ilistcreateparsingxml
Problem
Here's a class I created to parse XML to create an IList.
It seems to me that this is something that will be done a lot in general by a lot of different programs. Is there a more standard way of doing this using .Net and C#
```
///
/// public class CListFromXml {}
/// Created By: Michael Fitzpatrick, 2012 Sept 25
/// Please attribute & send email: samplecode AT mikefitz dot us
/// Create an IList from XML data
/// XML Syntax -
///
/// (data definition)
///
///
/// Type: System.Double by default
/// Type can be Enum using the following syntax:
/// Type="NameSpace.ClassParent.Class+EnumName, ClassParent"
/// See: http://stackoverflow.com/questions/2493215/create-list-of-variable-type
///
/// Enabled: true by default, If false: no list is created
///
/// Iterations: 1 by default, If 0: no list is created.
/// If LT 0 then Iterations = Int32.MaxValue
///
/// (data definition): As CSV list-
/// (item), (item), ...
/// where:
/// (item) is any value. For arrays item is bracketed using "{}"
/// just like C# uses for initializers. Example:
///
/// {1960, 1970, 1980},
/// {2960, 2970, 2980},
/// {2960}
///
///
/// (data definition): As Center / StepDelta -
///
/// (some value)
/// (some value)
/// (some integer)
/// (some integer)
///
///
public class CListFromXml
{
public readonly IList Data = null;
public readonly string Name;
public readonly bool Enabled;
public readonly Type Type;
public readonly string TypeName;
public readonly Int32 Iterations;
private static bool ReadCsv(Type typ, string csvData, IList lst)
{
string[] astr = csvData
.Split(new[] { ",", "\r\n", "\n", " ", "\t", "}" }
, StringSplitOptions.RemoveEmptyEntries);
if (astr.Length == 0)
return false;
if (typ == typeof(bool))
foreach (str
It seems to me that this is something that will be done a lot in general by a lot of different programs. Is there a more standard way of doing this using .Net and C#
```
///
/// public class CListFromXml {}
/// Created By: Michael Fitzpatrick, 2012 Sept 25
/// Please attribute & send email: samplecode AT mikefitz dot us
/// Create an IList from XML data
/// XML Syntax -
///
/// (data definition)
///
///
/// Type: System.Double by default
/// Type can be Enum using the following syntax:
/// Type="NameSpace.ClassParent.Class+EnumName, ClassParent"
/// See: http://stackoverflow.com/questions/2493215/create-list-of-variable-type
///
/// Enabled: true by default, If false: no list is created
///
/// Iterations: 1 by default, If 0: no list is created.
/// If LT 0 then Iterations = Int32.MaxValue
///
/// (data definition): As CSV list-
/// (item), (item), ...
/// where:
/// (item) is any value. For arrays item is bracketed using "{}"
/// just like C# uses for initializers. Example:
///
/// {1960, 1970, 1980},
/// {2960, 2970, 2980},
/// {2960}
///
///
/// (data definition): As Center / StepDelta -
///
/// (some value)
/// (some value)
/// (some integer)
/// (some integer)
///
///
public class CListFromXml
{
public readonly IList Data = null;
public readonly string Name;
public readonly bool Enabled;
public readonly Type Type;
public readonly string TypeName;
public readonly Int32 Iterations;
private static bool ReadCsv(Type typ, string csvData, IList lst)
{
string[] astr = csvData
.Split(new[] { ",", "\r\n", "\n", " ", "\t", "}" }
, StringSplitOptions.RemoveEmptyEntries);
if (astr.Length == 0)
return false;
if (typ == typeof(bool))
foreach (str
Solution
Your code is definitely not a standard way of parsing XML in .NET. Usually there are 2 main cases where XML parsing is needed:
-
You expect a predefined XML structure. In this case all the types of XML nodes are known upfront, so there is no need to specify them inside XML (as you do), and the easiest way to parse such XML is to create classes that correspond to the XML structure and use
Also I usually create a corresponding XSD that specifies the structure of XML that I expect, and if I don't trust the source of XML I run XSD validation before deserializing the XML into objects. The XML structure that you described (where lists are stored enclosed in curly brackets) can't be validated correctly using XSD.
There is a variation of this option when some .NET types are not known in advance, but the structure is still fixed, e.g. if you store the information about solution plugins (and the type of plugin is stored in XML). In that case I would either deserialize the XML into objects (leaving extension points as
-
You receive an unstructured XML and want to traverse it looking for familiar nodes, patterns, or anything else. In this case proper solution would be to use
I wouldn't recommend using the code that you provided in production as it tries to use XML in its own unique way, which means you won't get support from existing frameworks, and other clients that may want to generate XML consumed by your service would have to design their own hacks. Also the code itself is quite unreadable due to nested conditions and large methods, consider refactoring it. And it's not recommended to expose class fields as public, even if they are readonly.
-
You expect a predefined XML structure. In this case all the types of XML nodes are known upfront, so there is no need to specify them inside XML (as you do), and the easiest way to parse such XML is to create classes that correspond to the XML structure and use
XmlSerializer to do the work. Also I usually create a corresponding XSD that specifies the structure of XML that I expect, and if I don't trust the source of XML I run XSD validation before deserializing the XML into objects. The XML structure that you described (where lists are stored enclosed in curly brackets) can't be validated correctly using XSD.
There is a variation of this option when some .NET types are not known in advance, but the structure is still fixed, e.g. if you store the information about solution plugins (and the type of plugin is stored in XML). In that case I would either deserialize the XML into objects (leaving extension points as
XmlElement's) and then resolve .NET types for plugins, or create a custom IXmlSerializable implementation that loads appropriate types.-
You receive an unstructured XML and want to traverse it looking for familiar nodes, patterns, or anything else. In this case proper solution would be to use
XDocument or XmlDocument, or even XmlReader if you need maximum performance or expect large documents.I wouldn't recommend using the code that you provided in production as it tries to use XML in its own unique way, which means you won't get support from existing frameworks, and other clients that may want to generate XML consumed by your service would have to design their own hacks. Also the code itself is quite unreadable due to nested conditions and large methods, consider refactoring it. And it's not recommended to expose class fields as public, even if they are readonly.
Context
StackExchange Code Review Q#15922, answer score: 3
Revisions (0)
No revisions yet.