I have a quite elaborate XML I have been able to parse most of it however im coming across a tree that just has me stumped and im afraid that I'm making harder then it needs to be. here is the XML I'm referring to.
<Codes>
<CustomFieldValueSet name="Account" label="Account" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>7200</Value>
<Description>General Supplies</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>7200</Value>
<Description>General Supplies</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>7200</Value>
<Description>General Supplies</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Activity" label="Activity" distributionType="PercentOfPrice" />
<CustomFieldValueSet name="Chart" label="Chart" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>T</Value>
<Description>University</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>T</Value>
<Description>University</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>T</Value>
<Description>University</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Fund" label="Fund" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>360806</Value>
<Description>National Institutes of Health</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>360903</Value>
<Description>National Institutes of Health</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>360957</Value>
<Description>National Institutes of Health</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Program" label="Program" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>02</Value>
<Description>Research</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>02</Value>
<Description>Research</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>02</Value>
<Description>Research</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Location" label="Location" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>015</Value>
<Description>Biology - Life Science</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>015</Value>
<Description>Biology - Life Science</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>015</Value>
<Description>Biology - Life Science</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Organization" label="Organization" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>04400</Value>
<Description>TUSM:Neuroscience</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>04400</Value>
<Description>TUSM:Neuroscience</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>04400</Value>
<Description>TUSM:Neuroscience</Description>
</CustomFieldValue>
</CustomFieldValueSet>
</Codes>
I'm trying to end up with a list the would look something like this.
Account distributionType Activity distributionValue Fund
7200 PercentOfPrice "" 10 360806
7200 PercentOfPrice "" 45 360903
7200 PercentOfPrice "" 45 360957
etc...
I have written code the looks something like this. Here is a snippet. Mind you I think i have over complicated this.
if (tagName == "Codes")
{
// Create another reader that contains just the accounting elements.
XmlReader inner = reader.ReadSubtree();
//inner.ReadToDescendant("Codes");
//printOutXML(inner);
while (inner.Read())
{
switch (inner.NodeType)
{
//walk down the xml hiearchy then simply fill in the values.
case XmlNodeType.Element:
switch (reader.Name)
{
case "CustomFieldValueSet":
//get the attribute that we are currently working with such as account and
innerTagName=inner.GetAttribute("name");
// activity and location can potentially be blank therefore i will check here if it is
//and if it is i will immediate assign the activity list a set of empty quotes.
if (innerTagName == "Activity")
{
if (inner.IsEmptyElement)
{ //quickly put fillers in .
for (int i = 0; i < thisInvoice.account.Count; i++)
{
thisInvoice.activity.Add("");
}
}
}
if (innerTagName == "Location")
{
if (inner.IsEmptyElement)
{ //quickly put fillers in .
for (int i = 0; i < thisInvoice.account.Count; i++)
{
thisInvoice.location.Add("");
}
//thisInvoice.activity.Add("");
}
}
if (null == inner.GetAttribute("distributionType"))
{
distType = null;
}
else if
(distributionSwitch == false)
{
thisInvoice.distributionType.Add(inner.GetAttribute("distributionType") ?? "");
distType = inner.GetAttribute("distributionType") ?? "";
}
//Console.WriteLine(inner.Value);
//Console.WriteLine(inner.Name);
break;
case "CustomFieldValue":
if(null == inner.GetAttribute("distributionValue"))
//thisInvoice.distributionValue.Add(inner.GetAttribute("distributionValue") ?? "");
{/*do nothing*/}
else if
(distributionSwitch == false)
{
thisInvoice.distributionValue.Add(inner.GetAttribute("distributionValue") ?? "");
}
//check the length of the current distribution if the lenght is less than the curren distribution value
// then we must then add the values to the new location.
if (thisInvoice.distributionValue.Count > thisInvoice.distributionType.Count)
{
for (int i = 0; i < thisInvoice.distributionValue.Count - thisInvoice.distributionType.Count; i++)
{
thisInvoice.distributionType.Add(distType);
}
}
break;
case "Value":
// XmlNodeType.Text
if (innerTagName == "Account"/*&& inner.NodeType ==XmlNodeType.Text*/)
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.account.Add(inner.Value);
}
if (innerTagName == "Activity")
{
// activitiy is not a mandartory field so it could be empty therefore we need
// to check if its a self closing tag and if it is then we need to assign and
if (inner.IsEmptyElement)
{
thisInvoice.activity.Add("");
}
else
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.activity.Add(inner.Value);
}
}
if (innerTagName == "Location")
{
if (inner.IsEmptyElement)
{
thisInvoice.location.Add("");
}
else
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.location.Add(inner.Value);
}
}
if (innerTagName == "Fund")
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.fund.Add(inner.Value);
}
if (innerTagName == "Organization")
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.org.Add(inner.Value);
}
if (innerTagName == "Program")
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.prog.Add(inner.Value);
}
break;
}//end switch
break;//brake the outside case.
case XmlNodeType.EndElement:
if (inner.Name == "CustomFieldValueSet" || inner.Value == "CustomFieldValueSet")
{
distributionSwitch = true;
Console.WriteLine(reader.Value);
Console.WriteLine(reader.Name);
}
if (inner.Name == "Codes")
{
distributionSwitch = false;
distType = null;
inner.Close();
}
break;
}//end switch
}//end while
}//end the if;
In the case of the tag distributionType i need to make the list length as long as the list for account so in other words once i have it on a variable i need to use it as a filler to make the distribution type list as big as the account list. I cant imagine that there is not an easier way to do this I keep looking at linq to xml but it does not make much sense. I would love to hear how some of you experts would tackle this one. I'm trying to put together an elegant solution with a little less code. Any help would be greatly appreciated.