Complete novice here so please be patient. I work with XML on a daily basis but C# is something which i'm trying hard to learn as I know it will be useful in my job.
I've been playing with various methods and classes in the Xml namespace and i am just trying to perform some basic functions - for practice more than anything.
Firstly, i seem to be able to read my XML documents using XmlReader without too much trouble and can output to a console window (very basic stuff). I now want to find each occurance of the element "refdm" within the document and output just the content of the child elements of "refdm" (not the xml elements as well). So the following example is an extract of the XML (full file attached) and i basically want to pull the content of the text that is contained within the child nodes of the 'refdm' elements, and do this for each occurance of refdm, so that i have a nice list of all the refdms in the document.
<title>Location</title> <para>There is a Shore Connection Box installed on each side of the ship, each with its own compartment on Deck 4 at the RAS point. They are connected on the Interconnectors between the Forward Starboard Switchboard (HVSB1) and each of the Aft Switchboards (HVSB3 and HVSB4). The compartments remain closed when the ship is at sea. For manufacturers manual refer to data module <refdm><avee><modelic>CVF0000000QUE</modelic> <sdc>AAA</sdc><chapnum>AD2</chapnum><section>0</section><subsect>5</subsect> <subject>30</subject><discode>00</discode><discodev>00</discodev> <incode>031</incode><incodev>A</incodev><itemloc>A</itemloc></avee> </refdm>.</para>
With the following code i can output each occurance as required, but it also displays the xml tags of the child elements, which i don't want:
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader xmlReader = XmlReader.Create("C:\\Documents and Settings\\S0035651\\Desktop\\XML_test.xml", settings);
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "refdm"))
{
try
{
Console.WriteLine("DM Code: " + xmlReader.ReadInnerXml());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
Console.ReadLine();
I'm aware that beacuse i'm using the ReadInnerXml method that i'm asking to display the xml elements as well but i cannot work out an alternative.
A foreach loop using XmlDocument seems like a sensible option but i can't seem to get that to work either. XmlDocument may be a better option but i get errors when it attempts to read the document.
As I said, complete novice with C#. Any pointers or advice would be appreciated.
Attached File(s)
-
XML_test.xml (13.4K)
Number of downloads: 19

New Topic/Question
Reply



MultiQuote



|