Tuesday, 15 April 2014

Reading repetitive XML to memory using C#

Sample XML Document:
<Elements>
    <Element>
        <Name>N1</Name>
        <Type>T1</Type>
        <Color>C1</Color>
    </Element>
    <Element>
        <Name>N2</Name>
        <Type>T2</Type>
        <Color>C2</Color>
    </Element>
    <Element>
        <Name>N3</Name>
        <Type>T3</Type>
        <Color>C3</Color>
    </Element>
</Elements>


Sample C# Code:
XmlDocument myDoc = new XmlDocument()
myDoc.Load(XML_File_Path_Name);

foreach(XmlElement elem in myDoc.SelectNodes("Elements/Element"))
{
    XmlNode nodeName = elem.SelectSingleNode("Name/text()");
    XmlNode nodeType = elem.SelectSingleNode("Type/text()");
    XmlNode nodeColor = elem.SelectSingleNode("Color/text()");

    string name = nodeName!=null ? nodeName.Value : String.Empty;
    string type = nodeType!=null ? nodeType.Value : String.Empty;
    string color = nodeColor!=null ? nodeColor.Value : String.Empty;



No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...