Hi,
This is probably a simple question for all you gurus....
But I am still learning XML processing.
I have a function as follows to create a list of parts from an XML file (this
is based on a solution I found on the web).
static List<Part> LoadParts(string partDataFile)
{
using (Stream stream = GetResourceStream(partDataFile))
using (XmlReader xmlRdr = new XmlTextReader(stream))
return
(from partElem in
XDocument.Load(xmlRdr).Element("parts").Elements("part")
select Part.CreatePart(
(string)partElem.Attribute("prt"),
(string)partElem.Attribute("prtDesc"),
(string)partElem.Attribute("comment")
)).ToList();
}
My XML file is like this:
<?xml version="1.0" encoding="utf-8" ?>
<parts>
<part prt="92201546" prtDesc="a long part desc" comment="This can be a
comment" />
<part prt="90116842" prtDesc="wingnutfluegelmutter" comment="no comment" />
<part prt="91503604" prtDesc="trim panel a" comment="" />
</parts>
This works fine. However, I now need to extend my XML file to include some
"sub elements":
<?xml version="1.0" encoding="utf-8" ?>
<parts>
<part prt="92201546" prtDesc="a long part desc" comment="This can be a
comment">
<stores>
<store>6045B</store>
</stores>
<pous>
<pou>GA-T108R</pou>
</pous>
</part>
<part prt="90116842" prtDesc="wingnutfluegelmutter" comment="no comment" >
<stores>
<store>6045B</store>
</stores>
<pous>
<pou>GA-F211L</pou>
</pous>
</part>
<part prt="91503604" prtDesc="trim panel a" comment="" >
<stores>
<store>6072B</store>
<store>5081K</store>
<store>8147N</store>
<store>PLAS2</store>
</stores>
<pous>
<pou>GA-F102L</pou>
<pou>GA-T108R</pou>
<pou>BN-D04R</pou>
</pous>
</part>
</parts>
The new create function looks like this:
public static Part CreateCompletePart(
string partNumber,
string partDesc,
string comment,
List<string> stores,
List<string> ulocs)
{
return new Part
{
PartNumber = partNumber,
PartDesc = partDesc,
Comment = comment,
StoreLocs = stores,
POUs = ulocs
};
Can anyone point me in the direction of how I read the XML file and return the
List<string> into the function?
i.e. my LoadParts needs to look something like:
(from partElem in XDocument.Load(xmlRdr).Element("parts").Elements("part")
select Part.CreatePart(
(string)partElem.Attribute("prt"),
(string)partElem.Attribute("prtDesc"),
(string)partElem.Attribute("comment"),
(List<string>)partElem.Attribute("stores"),
(List<string>)partElem.Attribute(pous)
)).ToList();
But obviously, this doesn't work.
MICHAEL KING