Question

I received a complex XML file this week and it is schema based, but i didn't received any xsd file and i need to read each node of this one.

XML sample bellow:

<xc:XmlTest xmlns:xc="XmlTest" xmlns:mp="bs.TestParameters" xmlns:rt="bs.TestParameters.Parameter1" xmlns:rtcu="bs.TestParameters.Parameter1.Var">
<xc:XmlTestArea xc:value="TestParameters">
    <mp:Name xc:Value="raddesso" xmlns:mp="bs.TestParameters">
        <mp:Date xc:Value="20130215">
            <rt:RunTest xmlns:rt="bs.TestParameters.Parameter1">
                <rtcu:Var xmlns:rtcu="bs.TestParameters.Parameter1.Var">
                    <mp:FinalValue>1234</mp:FinalValue>
                </rtcu:Var>
            </rt:RunTest>
        </mp:Date>
        <mp:Date xc:Value="20130216">
            <rt:RunTest xmlns:rt="bs.TestParameters.Parameter1">
                <rtcu:Var xmlns:rtcu="bs.TestParameters.Parameter1.Var">
                    <mp:FinalValue>23234</mp:FinalValue>
                </rtcu:Var>
            </rt:RunTest>
        </mp:Date>
    </mp:Name>
</xc:XmlTestArea>
</xc:XmlTest>

This is only a sample of the true file, using fake data.

Is there any way to do a foreach on this nodes to find the FinalValue from each date?

Was it helpful?

Solution

You don't need a schema to read a file. The schema is only used to validate the file (to check integrity). But this step is optional.

To read the XML file. I suggest to use Linq-to-XML.

const string mpNamespace = "bs.TestParameters";

XDocument xDocument = XDocument.Load("C:/Path to the file.xml");

List<string> finalValues = (from dateNode in xDocument.Descendants(XName.Get("Date", mpNamespace))  // Gets all Date nodes in the document
                            from finalValueNode in dateNode.Descendants(XName.Get("FinalValue", mpNamespace))  // Gets all FinalValue nodes in the Date nodes
                            select finalValueNode.Value  // Gets the value of each FinalValue node
                           ).ToList();

Update

To return the Date and the FinalValue you can use anonymous types:

const string mpNamespace = "bs.TestParameters";
const string xcNamespace = "XmlTest";

XDocument xDocument = XDocument.Load("C:/Path to the file.xml");

var finalValues = (from dateNode in xDocument.Descendants(XName.Get("Date", mpNamespace))  // Gets all Date nodes in the document
                   from finalValueNode in dateNode.Descendants(XName.Get("FinalValue", mpNamespace))  // Gets all FinalValue nodes in the Date nodes
                   select new  // Creates an instance of an anonymous type with Date and FinalValue
                   {
                       Date = dateNode.Attribute(XName.Get("Value", xcNamespace)).Value,
                       FinalValue = finalValueNode.Value  // Gets the value of each FinalValue node
                   }
                  ).ToList();

OTHER TIPS

XmlDocument doc = new XmlDocument();
doc.Load("path to xml file");

XmlNodeList finalValues = doc.GetElementByTagName("FinalValue");

finalValues will be a list of node that has the tag name "FinalValue", you can then read the inner text and story in a list.

List<string> values = new List<string>();
foreach(XmlNode node in finalValues)
    values.Add(node.InnerText);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top