Click here to Skip to main content
15,900,816 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to write code for browse button Pin
ashok104231-Dec-08 23:57
ashok104231-Dec-08 23:57 
QuestionTextbox Event Pin
Nath30-Dec-08 19:46
Nath30-Dec-08 19:46 
AnswerRe: Textbox Event Pin
vaghelabhavesh31-Dec-08 8:20
vaghelabhavesh31-Dec-08 8:20 
QuestionPrint .rdlc Report [modified] Pin
zeeShan anSari30-Dec-08 18:45
zeeShan anSari30-Dec-08 18:45 
Questionzedgraph control Pin
hamidhakimi30-Dec-08 18:37
hamidhakimi30-Dec-08 18:37 
Questionzedgraph control Pin
hamidhakimi30-Dec-08 18:36
hamidhakimi30-Dec-08 18:36 
Questionmultiline textbox in datagridview Pin
SreejithKumar M30-Dec-08 18:18
SreejithKumar M30-Dec-08 18:18 
QuestionRecursion to find an XML element Pin
Steven D. Foster30-Dec-08 14:29
Steven D. Foster30-Dec-08 14:29 
I am missing something small here. I'm writing a simple DB data transfer utility using a custom XML file as the config source (this is the first step, obtaining the DB schema).

Recursion fries my brain Dead | X|

Thanks! to anyone that can spot the IF statement I'm missing.

XML File...
<DbSync>
<Schema>
<SqlConnection Host="10.1.1.1" Database="DBName" Username="username" Password="password" />
<Output Path="C:\DbSync.schema" />
</Schema>
</DbSync>

Code...
internal class Program
{
private const string FILENAME = "Testfile.xml";


private static void Main(string[] args)
{

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(FILENAME);

// This line WORKS and finds the first XML Element node with the name "Schema".
XmlNode configNode = FindNode("Schema", xmlDocument.FirstChild);

// This line WORKS and finds the "SqlConnection" node in the document.
XmlNode connectionNode = FindNode("SqlConnection", configNode);

// This line WORKS and finds "NULL" because the element "Test" doesn't exist.
XmlNode nullNode = FindNode("Test", configNode);

// This line DOESN'T WORK. Element "Output" exists but it is not returning it from the document.
XmlNode outputNode = FindNode("Output", configNode);


#region Display Data
Console.Write(" ConfigNode : " + CheckValue(configNode) + Environment.NewLine);
Console.Write("ConnectionNode : " + CheckValue(connectionNode) + Environment.NewLine);
Console.Write(" NullNode : " + CheckValue(nullNode) + Environment.NewLine);
Console.Write(" OutputNode : " + CheckValue(outputNode) + Environment.NewLine);

Console.Write(Environment.NewLine + "Press Any Key" + Environment.NewLine);
Console.ReadKey(true);
#endregion
}

#region Used for displaying to console.
private static string CheckValue(object obj)
{
return obj == null ? "*** NULL ***" : "CONTAINS VALUE";
}
#endregion


// TODO: FIGURE OUT WHY I DON'T WORK IF I AM SEARCHING FOR THE 2nd, 3rd, etc... CHILDNODE. Seems to only work with the first childnode.
private static XmlNode FindNode(string nodename, XmlNode node)
{
if (node.Name == nodename)
{
return node;
}
else
{
foreach (XmlNode childNode in node.ChildNodes)
{
// Uses recursion to transverse the nodes in the Xml doc.
// I think the error is here. It is always returning the first
// child element even if there is more than 1 child element.
// I just can't figure out how to get around that (To check ALL child nodes).
// I think I need some type of if-then here but can't put my finger on it...
return FindNode(nodename, childNode);
}
}
return null;
}
}
QuestionRe: Recursion to find an XML element [modified] Pin
Steven D. Foster30-Dec-08 14:52
Steven D. Foster30-Dec-08 14:52 
AnswerRe: Recursion to find an XML element Pin
Not Active30-Dec-08 15:55
mentorNot Active30-Dec-08 15:55 
GeneralRe: Recursion to find an XML element Pin
Steven D. Foster30-Dec-08 16:12
Steven D. Foster30-Dec-08 16:12 
GeneralRe: Recursion to find an XML element Pin
Not Active30-Dec-08 16:33
mentorNot Active30-Dec-08 16:33 
GeneralRe: Recursion to find an XML element Pin
Steven D. Foster31-Dec-08 4:14
Steven D. Foster31-Dec-08 4:14 
AnswerRe: Recursion to find an XML element Pin
Steven D. Foster30-Dec-08 16:55
Steven D. Foster30-Dec-08 16:55 
AnswerRe: Recursion to find an XML element Pin
Christian Graus30-Dec-08 18:01
protectorChristian Graus30-Dec-08 18:01 
GeneralRe: Recursion to find an XML element Pin
Steven D. Foster31-Dec-08 4:07
Steven D. Foster31-Dec-08 4:07 
AnswerRecursion = BAD to find an XML element Pin
Steven D. Foster31-Dec-08 4:45
Steven D. Foster31-Dec-08 4:45 
QuestionHow do i go about this... do i create a DataBind / DataSet ??? [modified] Pin
cobalt-rose30-Dec-08 12:02
cobalt-rose30-Dec-08 12:02 
AnswerRe: How do i go about this... do i create a DataBind / DataSet ??? Pin
Not Active30-Dec-08 13:23
mentorNot Active30-Dec-08 13:23 
AnswerRe: How do i go about this... do i create a DataBind / DataSet ??? Pin
Mycroft Holmes30-Dec-08 14:45
professionalMycroft Holmes30-Dec-08 14:45 
GeneralRe: How do i go about this... do i create a DataBind / DataSet ??? Pin
cobalt-rose1-Jan-09 13:25
cobalt-rose1-Jan-09 13:25 
QuestionApp.config Pin
s196675m30-Dec-08 11:34
s196675m30-Dec-08 11:34 
AnswerRe: App.config Pin
vaghelabhavesh30-Dec-08 13:08
vaghelabhavesh30-Dec-08 13:08 
QuestionDesign time collection problem Pin
kanchoette30-Dec-08 10:16
kanchoette30-Dec-08 10:16 
AnswerRe: Design time collection problem Pin
Wendelius30-Dec-08 10:44
mentorWendelius30-Dec-08 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.