How to analyze values ​​of union and list types in .NET?

I have an XML schema that includes data types that use <xs:union>and <xs:list>. Here is an excerpt:

<xs:simpleType name="mixeduniontype">
  <xs:union memberTypes="xs:boolean xs:int xs:double xs:string"/>
</xs:simpleType>

<xs:simpleType name="valuelist">
  <xs:list itemType="xs:double"/>
</xs:simpleType>

And here is a sample XML fragment:

<value>42</value>
<value>hello</value>

<values>1 2 3.2 5.6</values>

The top two <value>are joins, and the bottom <values>is a list.

My question is how I analyze the elements <xs:union>and <xs:list>in .NET?

How to check which data type matters in a join element?

How to extract items in a list item and convert them to a C # list?

Is there any built-in support in System.XML for this kind of parsing, or do I need to write the parsing code myself?

+5
1

,

, .

xs:list xs:union, , .

//assuming parent is the containing node

//Parse your 'valuelist'
var newList = new List<double>();
foreach (string s in parent.XElement("values").value.Split(" ")) //should check for nulls here
{
    double value = 0.0;
    if (double.TryParse(s, value))
    {
        newList.Add(value);
    }
    else
    {
        \\throw some format exception
    }
}

//Parse your 'mixeduniontype'
Type valueType = typeof string;
double doubleValue;
int intValue;
boolean booleanValue;

var stringValue = parent.XElement("value").First();

if (double.TryParse(stringValue, doubleValue))
{
    valueType = typeof double;
}
else
{
    if (int.TryParse(stringValue, intValue))
    {
        valueType = typeof int;
    }
    else
    {
        if (bool.TryParse(stringValue, booleanValue))
             valueType = typeof boolean;
    }
}
0

All Articles