|
I have to find out the maximum number of nodes for a child node
ex. my Xml is
<customers>
<customer id="c1">
<name>aaaa</name>
<job>Plumber</job>
<city>Hdddd</city>
<state>aaaaa</state>
</customer>
<customer id="c2">
<name>hhhhhhhh</name>
<job>hdfgdfgdf</job>
<city>dfgdfg</city>
<state>dfgdf</state>
</customer>
<customer id="c3">
<name>Memphis rays</name>
<job>
Car guy
<designation>dfsdfsd</designation>
</job>
<city>dfgdfgdfg</city>
<state>fgdfgdf</state>
</customer>
</customers>
Now the third customer node has a child to it's child (customer/job/designation) I need to find out that node, that has maximum children.. by reading the Xml file through C#.
Help!
<b>_____________________________________________________</b>
<b>Yea! I could be wrong...</b>
|
|
|
|
|
I think the best solution for this problem is to get the complete XML file with following instructions:
**************************************************
private static XmlDocument doc = new XmlDocument();
public XmlNode root;
using (StreamReader myTempFile = new StreamReader(xmlFile))
{
//read complete File into string
while ((rl = myTempFile.ReadLine()) != null)
{
tmpString += rl;
}
}
doc.LoadXml(tmpString);
root = doc.DocumentElement;
*************************************************
Now you have your complete XML-file in 'doc'. Additionally you have a root node.
Now you have to go through every node and check how many childs are available.
Something like:
*************************************************
int maxChildCount = 0;
int maxNodePos = 0;
int actNodePos = 0;
while (actNode != null)
{
actNodePos++;
int actChildCount = actNode.ChildNodes.Count;
if (maxChildCount < acthildCount)
{
maxchildCount = actChildCount;
maxNodePos = actNodePos;
}
actNode = actNode.NextSibling;
}
*************************************************
At the end you have the maxNodePos and the maxChildCount. At least you have to do is to start with your root-node and to step forward with 'actNode = actNode.NextSibling;' until your maxNodePos is reached.
I think it´s one solution and there are other possible, maybe faster to.
But I think it´s a solution.
Enjoy it, your
Norman-Timo
|
|
|
|
|
Yes thanx for the idea but the code is not working. I want to know the node that has only the root element as it's parent. I used this recusive fun.
public void readXml(XmlTextReader textReader)
{
string temp;
while(textReader.Read())
{
temp = textReader.Name;
n++;
Console.WriteLine(temp);
readXml(textReader);
}
}
but I want write down (Console.Write(...)) only for that node which has the max no. of child nodes.
Any more Ideas?
Please
_____________________________________________________
Yea! I could be wrong...
|
|
|
|
|
Ok if I understand it correctly:
You want to have first level Nodes and especially that one with the most child nodes?
In your case the nodes with "customer id..."?
But than you can take the first part from my example (the part until you have the 'doc' XML file and your root node.
Than you can do it in an analogue way:
***************************************************
int maxChildCount = 0;
XmlNode mostChildNode = null;
actNode = root.FirstChild; // then you have the first customer node!
while (actNode != null)
{
if (maxChildCount < actNode.ChildNodes.Count)
{
maxChildCount = actNode.ChildNodes.Count;
mostChildNode = actNode;
}
actNode = actNode.NextSibling;
}
*************************************************
The reason that my program is not working is maybe the fact, that I typed in the source directly in this reply-form (sorry).
But you are certainly able to correct compiler errrors, I think. (if there are some)
I think with these line of code you have your solution, if I understand you right.
I´m sorry if I´m not correct again.
Norman-Timo
Edited:
By the way with what html tags can I highlite the code sources?
|
|
|
|
|
thanx I still have to try the second part, but I think loading the whole Xml document in a string in uneccary.. (I think). You can use the xmlTeaxtReader.Read(); method in the while loop right?
_____________________________________________________
Yea! I could be wrong...
|
|
|
|
|
XPath!
eg XmlDocument.SelectNodes("nodename");
XPath syntax is beyond the scope of a forum post, I suggest you get the docs from the w3c. Its suprisingly not difficult and just looking at few examples will get you on the way.
Note: if you gonna using xmlns, then u need to specify an explicit prefix for the Xpath to work, and pass an XmlNamespaceManager instance to the function.
top secret xacc-ide 0.0.1
|
|
|
|
|
ok thanx.. I know a little bit of XPath, but I hav'nt used it in C#
Any tutorials on using it in C# please.
_____________________________________________________
Yea! I could be wrong...
|
|
|
|
|
The problem I'm facing is as follows:
I have two classes: cBiquad and cFilter.
The cBiquad class has five properties that have get/set capabilities.
In the cFilter constructor, I have defined an array of cBiquad objects as follows:
cBiquad[] arrBiquads = null;
for ( i = 0; i < mnBiquads; i++ )
{
arrBiquads[i] = new cBiquad();
}
Now in one of the methods in the cFilter class - SetParameters(), no arguments - I need to set the five properties of the cBiquad objects.
However, when I attempt to do the following:
for ( i = 0; i < mnBiquads; i++ )
{
arrBiquads[i].Property1 = Prop1Value; //etc
}
I receive the following error message:
The name 'arrBiquads' does not exist in the class or namespace 'DSP.cFilter'
What should I do to get around this?
|
|
|
|
|
put this statement
<br />
cBiquad[] arrBiquads = null;<br />
outside of the constructor, instead of inside it. If it's inside the constructor it will only
be visible to code also in constructor.
<br />
public cFilter()<br />
{<br />
arrBiQuads = new cBiQuad[mnBiquads];<br />
for ( i = 0; i < mnBiquads; i++ )<br />
arrBiquads[i] = new cBiquad();<br />
}<br />
Hope it works better now..
|
|
|
|
|
If you define the array of cBiquad objects in the constructor it's only a local variable and no longer accessible as soon as the constructor scope is left.
You have to define a class variable for the array.
You will also have to initialize your array before you create it's members.
cBiquad[] arrBiquads = new cBiquad[mnBiquads];
for ( i = 0; i < mnBiquads; i++ )
{
arrBiquads[i] = new cBiquad();
}
www.troschuetz.de
|
|
|
|
|
Is there a designer like "the MMC Snap-in Designer for VB6" for any .NET environment?
|
|
|
|
|
hello
i am looking for a math algorithm or a code in c# to find a specific floating number in floating number limit and this in minimum tries(somthing short smart and simple)
thanks tzahi
|
|
|
|
|
hi ,
Ur question is incomplete. u have given the Range of ur output. ( within the limits of floating point values ). What are ur domains ? ( input ).
What u want exactly .
Praseed Pai
www.praseedpai.com
|
|
|
|
|
I'm trying to port some old C++ code to C#.
I have a few classes that were defined in a ".h" file. Their constructors and member variables were defined in the corresponding .cpp file.
What is the best way to port this file to C#. More specifically:
1. I assume that the class, its constructor and methods are all defined together as follows:
class DSP
{
// constructor
// methods
}
Is this correct?
2. The original C++ listing in the .h file is as follows:
class DSP
{
public:
// public variables
protected:
//constructor
// destructor
// private variables
void SomeMethod()
};
These public variables need to be properties that can be assigned to. Therefore in the C# implementation, do I need to define these fields with "get" and "set" properties?
I know these are very elementary questions, but I haven't ported any C++ code over to C# before, and I appreciate your help!
|
|
|
|
|
C# classes do not have header files, pretty much anything in the header can be discarded. The declaration of functions in C# is the same as the definition, and everything can automatically see the namespaces in the project, without having to include anything.
Yes, the best way to set properties is with get/set methods, then you can make variables that can be got and not set, and vice versa, if needed.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I have made a table using access , one of its coulumns is of the type
Hyperlink. I can't access it using Data grid on C#.
where I want to make it a link to a certain file or to read the file contentes of a certain row
|
|
|
|
|
Hello:
I recently changed my code around... being inexperienced and stupid, I had put code that replaced the connectionstring of an OleDbAdapter in the region that the designer obliterates when you make changes. But it worked.
Now I have moved the code to the form load event function, and the program cannot fill the dataset. I do not understand why at all. I have put in MessageBox.Show calls to show that the connectionstring is alright, so I am confused now as to what the problem is. Any ideas?
|
|
|
|
|
Try putting the code block in the constructor just after InitializeComponent()
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
}
}
Hope this works.
|
|
|
|
|
hi ,
Please try to execute your code block in try....catch block. And send the error message.
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
1.Your suggestion is correct though What I wanted to stress that one should not write any code other than that of GUI design in the InitializeComponent(). Other code [non GUI related] gets deleted.
2.Whether you put the code chunk in try catch block or not if there is any error it will pop up. If it is not handled it will get crashed.
3.Also he has said, he is not getting any error pop-up msg.
Lets see what is Chris's problem? He has not reverted back till now.....
|
|
|
|
|
Hi,
My main app need to call a static method on several different classes, so each class (A and B) inherited a base class (BASE) that defined and implemented (empty) a static method so that the main app could do this
doSomething()<br />
{<br />
((BASE)child).TheStaticMethod(...)<br />
}
Other methods were also defined as abstract, and had to be implemented by A and B.
Now I wanted things a bit "cleaner" so I created an interface that classes A and B inherited from and thus had to implement, which works just fine for all other methods than the static one. You cant say
Interface IBase<br />
{<br />
static void ThePreviouslyStaticMethod();<br />
}
- as you get a "error CS0106: The modifier 'static' is not valid for this item" from the compiler.
Now what do I do ? Isn't it possible to force classes to implement a certain static method ?
Thanks in advance
/Jan
Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert
|
|
|
|
|
I'm using DX9, which has lots of lovely C# support, but only C++ docs, it seems. The C++ docs talk about being able to read ID3 tags, I can't find that support in C#. Can anyone help ?
Thanks.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
|
You are my hero
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Have you succeeded in downloading this ? I've had it time out at about 180 MB 7 or 8 times.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|