|
I want to iterate thru an xml file and I'm not having any luck. I'm very new to C# so I don't know if this is even possible, but it seems to me that it should be.
I want to display the parent nodes of an xml file in a combobox. Is that so difficult? There's plenty of examples when dealing with SQL Server or Access, but is it possible to get the same functionality via an xml file?
As soon as a particular parent node is selected, I want the datagrid on the form to update itself with the selected parent node (from the combobox).
I've looked at XPath queries but I don't see how that would work in this case.
Any ideas?
|
|
|
|
|
By parent nodes I assume that you mean the elements that are direct children of the root element. Then you just get the nodes in the root element an iterate through them, adding an item in the combobox for each node.
Something like:
foreach (XmlNode node in myXmlDoc.DocumentElement.ChildNodes) {
myCombo.Items.Add(node.whatever);
}
---
b { font-weight: normal; }
|
|
|
|
|
Thanks Guffa,
That helped a lot, atleast I saw a little action from the combobox. I'm sure my XML file might need a little work though.
Here's the XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book>
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title>XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<price>49.99</price>
</book>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
I still can't get the datagrid to show only one particular book at a time. Right now it just shows all of them. Also, the combobox displays "book" 4 times.
Here's some code:
private void btnLoad_Click(object sender, System.EventArgs e)
{
opener.Title="Load XML Data";
opener.Filter="XML files(.xml)|*.xml";
if(opener.ShowDialog()==DialogResult.OK)
{
string myData=opener.FileName;
DataSet ds=new DataSet();
ds.ReadXml(myData);
XmlDataDocument xdd=new XmlDataDocument(ds);
xdd.SelectNodes("/bookstore/..");
foreach(XmlNode xnod in xdd.DocumentElement.ChildNodes)
{
cboBookName.Items.Add(xnod.LocalName);
}
dataGrid1.DataSource=ds;
dataGrid1.DataMember="book";
}
}
This is almost working, except for combobox displaying "book" 4 times over and the datagrid showing everything instead of just info for selected combobox item.
Sorry for the long post
Regards
Dan
|
|
|
|
|
Instead of just using the node name, locate a child of the node with a specific node name, for an example "title".
As you have put the data in a DataSet anyway, it might be easier to loop through that instead of the xml document, or just use the DataSet as data source for the combobox.
Create a DataView for the DataSet to use as data source for the grid. Use the RowFilter property of the DataView to filter out one specific item.
---
b { font-weight: normal; }
-- modified at 14:01 Sunday 25th September, 2005
|
|
|
|
|
Hi Guffa,
I'm still not able to place only the book titles in the combobox and details in the datagrid.
When I set the datasource of the combobox to the dataset (which contains my xml file) I get some kind of datalistmanager line displayed in the combobox but that's it. Sorry, right now I'm at work and I'm away from my code.
I guess what I'm having problems with is how to segment out part of the dataset to the combobox.
Dataviews would work? Is it better to use a Dataset in this case instead of an XmlDocument?
|
|
|
|
|
Hi there....Does any body know how to capture images/video clips through a webcam/scanner in C#.
Thanks
The way
|
|
|
|
|
|
Hi,
Thanks for your advice!
|
|
|
|
|
Thanks Andrew!
|
|
|
|
|
Hello!
Can someone please explain me how to get the IShellFolder interface of a subfolder after calling IShellFolder.EnumObjects() and then IEnumIDList.Next() (an example will be helpful). The interface I have implemented is:
<br />
[ComImport]<br />
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]<br />
[Guid("000214F2-0000-0000-C000-000000000046")]<br />
public interface IEnumIDList<br />
{<br />
[PreserveSig]<br />
Int32 Next(UInt32 celt,<br />
[MarshalAs(UnmanagedType.LPArray)]<br />
IntPtr[] rgelt,<br />
out UInt32 pceltFetched<br />
);<br />
<br />
[PreserveSig]<br />
Int32 Skip(UInt32 celt);<br />
<br />
[PreserveSig]<br />
Int32 Reset();<br />
<br />
[PreserveSig]<br />
Int32 Clone(out IEnumIDList ppenum);<br />
}<br />
Thank you!
|
|
|
|
|
Hello
I need to Serialize an object containing a hashtable where each value in the hashtable is an object.
[Serializable]
class A{
public ushort[] UshortArray; // lets assume this points to an array
}
[Serializable]
class B{
public Hashtable HashOfA;
}
void Main(){
A A1 = new A();
A A2 = new A();
B B1 = new B();
B1.Init() // Set hashtable
B1.Add(1,A1); // add item
B1.Add(2,A2); // add item
byte[] Binary;
/////////////////////////////////
// Here I want to serialize B1 to binary format
//////////////////////////////////
B B2 = new B();
///////////////////////////////////////
// And ofcourse back to object B2
///////////////////////////////////////
}
I know how to do this where insted of <class a=""> I have <struct a="">. Is there a solution for objects ?
I'm using a MemoryStream and BinaryFormatter.
Gilad.
|
|
|
|
|
Shouldn't make any difference at all. What problem are you facing?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hello
I need to Serialize an object containing a hashtable where each value in the hashtable is an object.
[Serializable]
class A{
public ushort[] UshortArray; // lets assume this points to an array
}
[Serializable]
class B{
public Hashtable HashOfA;
}
void Main(){
A A1 = new A();
A A2 = new A();
B B1 = new B();
B1.Init() // Set hashtable
B1.Add(1,A1); // add item
B1.Add(2,A2); // add item
byte[] Binary;
/////////////////////////////////
// Here I want to serialize B1 to binary format
//////////////////////////////////
B B2 = new B();
///////////////////////////////////////
// And ofcourse back to object B2
///////////////////////////////////////
}
I know how to do this where insted of <class a=""> I have <struct a="">. Is there a solution for objects ?
I'm using a MemoryStream and BinaryFormatter.
Gilad.
|
|
|
|
|
How can I know if a Class has an instance object in Computer's memory ?
|
|
|
|
|
You mean you want to track how many instances of the class have been created so far? That's easy, just add a static variable that is incremented in the constructor. Something like
class Foo
{
static int counter;
public Foo() { counter++; ...}
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi!
This will give you the number of instances in the current AppDomain only.
If you start the program twice or have your class loaded into different AppDomains, the counter will be initialized to 0 for every AppDomain.
If you really want to know how many instances there are computer-wide, a different approach will be needed.
Regards,
mav
|
|
|
|
|
I have a web browser control on Win form, using Visual C#. I open a Word document in it using control.Navigate..., everythings works fine.
Now I want to get the text that is selected in the control, but nothing works. I try:
_web.Focus();
SendKeys.Send("^c");
but nothing is on a clipboard.
Also this:
_web.ExecWB(SHDocVw.OLECMDID.OLECMDID_COPY, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,ref refmissing , ref refmissing);
but nothing on the clipboard as well.
Anybody has the solution on how to get the text that is selected in the web browser control?
Thanks.
|
|
|
|
|
Hi I have seen the similar problem in my application too. If anyone knows, please let us know. TIA.
|
|
|
|
|
hello everybody!
Please i want to develop editor for C# of my own and i want to integrate the cache of the dot net studio in my editor like when i want for example to open static method in a class for example.
<br />
int my_number = int.Parse("String to parse");<br />
When i write int and follow it with dot it gives me all avialable static methods for this class or when i want to construct new class by reference for example
<br />
Connection myconnection = new connection();<br />
I want when i refer to myconnection and follow it with dot to give me all avialable methods for this class "Dot notation" in vsstudio i know that this is involved for the cache of the studio but i want to use this cache and even when i want to create html file to include also this cache with dot notation . the same as the studio i know that it is easy to do but i don't know how it will work
I implemented full compiler for vsstudio family but i want to use this cache also with dot notation in this project any help about this please .even some programmers do it before isharpdevelop.net develop the same editor but i want to develop the same for my own programming any help about this please
Please reply to me please
Miss With The Best And Die Like The Rest
|
|
|
|
|
|
Code completion is really complicated.
When you want to provide it for classes and methods the user writes, you need to write your own C# parser.
The lookup rules can get really complicated. Your example is easy, you just need to use your parser to find the local variable, but what if it's a field or property from an inherited class?
What if it's a method call of an overloaded method? (Math.Abs(6).<Completion for integer> but Math.Abs(6.0).<Completion for double>)
You can take a look at how I've done it for the SharpDevelop IDE[^].
-- modified at 3:52 Sunday 25th September, 2005
|
|
|
|
|
hi to all,
as part of my apps startup i would like to test for local LAN connectivity as it uses tcp protocol for remoting, this can obviously also include a VPN.
i would like to test for tcp connectivity, so i tried this to my server which i know responds to ping and i get FAIL.
is this the best way or can i check the network adapter instead, also the app may be run as a user who does not have priveleges to start stop services etc.,
can anyone please help?
kind regards,
g00fy
<br />
internal void CheckLANConnectivity()<br />
{<br />
this.initLabel.Text = "Checking Network Connectivity ... ";<br />
this.Refresh();<br />
<br />
try<br />
{<br />
TcpClient client = new TcpClient();<br />
client.Connect("10.0.0.1", 7);<br />
this.initLabel.Text = "Network Connectivity ... PASS ";<br />
}<br />
catch<br />
{<br />
this.initLabel.Text = "Network Connectivity ... FAIL ";<br />
}<br />
}<br />
|
|
|
|
|
Hi!
This won't work because ping uses ICMP echo request packages, not TCP connections to check for connectivity.
(Btw. you don't even have to try UDPClient, this won't work either.:->)
If you want the code for a real ping, take a look here[^]
Otherwise, you could try to connect to the real server object you're trying to use later on to check whether it exists and is reachable.
Regards,
mav
|
|
|
|
|
thank you,
i do try catch later for the remoting but at startup, the client doesnt know about any servers.
is it possible to broadcast for remoting servers? i am trying this at the moment ?
if i cant get it i will try your ping link and see how i go.
thank you for response
g00fy
|
|
|
|
|
simple, i used this
<br />
internal void CheckLANConnectivity()<br />
{<br />
this.initLabel.Text = "Checking Network Connectivity ... ";<br />
this.Refresh();<br />
<br />
string host = System.Configuration.ConfigurationSettings.AppSettings["host"];<br />
Debug.WriteLine("host from config file: " + host);<br />
<br />
IPHostEntry serverHE;<br />
try<br />
{<br />
serverHE = Dns.GetHostByName(host); <br />
this.initLabel.Text = "Network Connectivity ... PASS ";<br />
Debug.WriteLine("hostbyname: " + serverHE.ToString());<br />
}<br />
catch(Exception)<br />
{<br />
this.initLabel.Text = "Network Connectivity ... FAIL ";<br />
}<br />
}<br />
simple but works, just need to know its there
kind regards,
g00fy
|
|
|
|