Link to home
Start Free TrialLog in
Avatar of Cam Kirmser
Cam Kirmser

asked on

What Does This C# Code Block Do?

Greetings.

I am a total C# neophyte. The last time I code in C was in 1995 and that was just for a computer class. I can read code a little but need help in deciphering just what this block of code does. Not so much the logic, but the syntax.

The code is;

        xd = new XmlDocument();
        String sourceFile = "i:\\XML_test_shortened.xml";
        xd.Load(sourceFile);
        XmlNodeList rows = xd.GetElementsByTagName("Row");
        foreach (XmlNode row in rows)
        {
            OutBuffer.AddRow();
            OutBuffer.Col1 = row.ChildNodes[0].ChildNodes[0].InnerText;
            OutBuffer.Col2 = row.ChildNodes[1].ChildNodes[0].InnerText;
            OutBuffer.Col3 = row.ChildNodes[2].ChildNodes[0].InnerText;
        }

Open in new window

I can see that it iterates through a file and, I think, transposes a single column of 3-row blocks into a 3-column array of n blocks (one row for every three of the original rows).

But, when I look at this code in SSIS, it gives me the following errors;

The name 'xd' does not exist in the current context
The name 'OutBuffer' does not exist in the current context

What do the errors mean? How do I fix them? Just what does the syntax of the code block mean?

Thanx for any help!
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

xd = new XmlDocument(); - here you create a new XmlDocument object. The error you are talking is because somehow you are missing xd declaration. Should be somewhere like this: 'XmlDocument xd;' or 'XmlDocument xd = null;'
       

String sourceFile = "i:\\XML_test_shortened.xml"; - this is a path to xml file.


        xd.Load(sourceFile); - this line - XmlDociument object reads (loads) content of an xml file.

        XmlNodeList rows = xd.GetElementsByTagName("Row"); - here we search all xml nodes with the name "Row". This call returns a list of XmlNode objects.

        foreach (XmlNode row in rows) - here we iterate through xml nodes
        {
            OutBuffer.AddRow(); - Not exactly sure what OutBuffer is but it looks like some custom object similar to a data collection similar to a DataTable or a list but not exactly.
here we get column names from xml nodes. It loks like xml nodes have some structure, i.e., they have children, and children have children as well. In this case it's better to see the xml file to understand the structure.
 
            OutBuffer.Col1 = row.ChildNodes[0].ChildNodes[0].InnerText;
            OutBuffer.Col2 = row.ChildNodes[1].ChildNodes[0].InnerText;
            OutBuffer.Col3 = row.ChildNodes[2].ChildNodes[0].InnerText;
        }

You are missing OutBuffer declaration as well. I'd guess somewhere in your code should be a special class for this object...
Avatar of Cam Kirmser
Cam Kirmser

ASKER

Thank you, anarki!

I've been doing searches since I posted the question and deduced what you posted about the "xd," but it is great to know that my deduction was correct.

As for the "OutBuffer" declaration, the guy who sent me the code block added that I need to "Change "OutBuffer" to "<what you named your output>Buffer." I've posted back to him on another forum, but he's not responded yet and I'm trying to get this figured out for a task at work.

I'm not at all sure what he means here, but I'm taking a stab that he means to put some sort of filename in place of "Out." That, or maybe a variable so that it can be used downstream in an SSIS Data Flow. However, your DataTable guess sounds pretty good; is that something standard? I'll try looking it up to see.

Is "ChildNodes" a keyword, or something I need to define?

Thanks again for the info!
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, anarki, I greatly appreciate it.

I discovered one of my problems. I'm doing this in SSIS and from an earlier suggestion to fix my problem. That had me using a Transformation Script Component and when I received the above code, I was still using the same task.

When I restarted the process from scratch, I used a Source Script Component and everything started to fall in place.

Outbuffer is a name that is set in the properties of the component task and now a lot of confusion has been cleared up.

Now, I'll have to study your code to cement, hopefully, that understanding in place.

Thanx!
Anarki has been a great help. I'm very pleased.