Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2003, 12:47 AM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# with XML. Urgent!Please Help!

:(

Dear All,

Let say I have the following xml file:-

<?xml version="1.0" standalone="yes"?>
<Items>
    <Books>
        <ISBN>123456</ISBN>
        <Title>ASP.NET</Title>
        <Publisher>Wrox</Publisher>
    </Books>
    <DVD>
        <Title>Princess Diary</Title>
        <Region>Asia</Region>
    </DVD>
    <Games>
        <Title>Super Mario</Title>
        <Platform>PC</Platform>
    </Games>
</Items>

Now, how can I do the following by using c#:-
1)how to count the total number of the elements (eg:books, dvd) in the xml file

2)how to read the element names (eg:books, games, dvd) and display it in a dropdownlist?

3)When the element name is being selected from the dropdownlist, how can we use c# to display all its attributes (eg:title, ISBN) ?

Thank you.
Your help is very very much appreciated.

Regards,
mec
 
Old September 24th, 2003, 05:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should check out the XmlDocument class. I have used this with positive experiences. You can do something like this in C#...

Code:
XmlDocument doc = new XmlDocument();
doc.Load("someXMLfile.xml");

foreach(XmlNode node in doc.DocumentElement.ChildNodes)
{
   switch(node.Name)
   {
      case "Books": 
        //something.
        break;
      case "DVD":
        //something.
        break;
      case "Games":
        //something.
        break;
      default: 
        //something.
        break;
  }
}
(The code above has not been tried)

Well, you have to explore the class members yourself! I have used DocumentElement above which gets the root node (Items) and then all of its child nodes.

Hope this helps

Jacob.
 
Old October 4th, 2003, 03:39 AM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

  What if I wanna add an element into the following xml file:-

<?xml version="1.0" standalone="yes"?>
<ItemField>
    <ISBN/>
    <Title/>
    <Publisher/>
    <Region/>
    <Platform/>
        <NewElementHere/>
</ItemField>

Thanks



 
Old October 5th, 2003, 02:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you are using Microsoft Visual Studio .NET you can find the information you need by typing 'XmlDocument' in the index part of the help, and then choose 'All members'! This will give you the possibilities... However you can do something like...

- Make the XmlDocument instance (e.g. like above).
- Create an element to insert.
- Set the attributes etc.
- If you want to write it back to the file use the Save method on the XmlDocument.


Hope this helps

Jacob.

 
Old October 5th, 2003, 10:12 PM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi jacob,

   too bad I am not using Visual studio .net. I am just using plain notepad to write the code. So, can you help me by giving me an example? for the second reply that i posted about adding a new element into an xml file?

Thank you so much

jayce

 
Old October 6th, 2003, 04:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I have to go to work now, so I havn't got much time, and therefore the example is perhaps a bit simple, but here it goes...
Code:
public void Page_Load(Object oSender, EventArgs e)
{
   String filename = Server.MapPath("books.xml");
   XmlDocument doc = new XmlDocument();
   try
   {
      doc.Load(filename);
      (doc.DocumentElement).AppendChild(doc.CreateElement("NewElement"));
      doc.Save(filename);        
   }
   catch(Exception exeption)
   {

   }
}
The books.xml file is naturally your file as it looks above. The DocumentElement retrieves the root which is ItemField in your case, and then AppendChild appends the new element as a child to the root. The new element is called NewElement in the above example.

Hope it helps Let me know.

Jacob.



 
Old October 6th, 2003, 10:59 PM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi...Jacob,

   thanks for your help!...Its wonderful. Where did u look for this syntax for adding xml element? Or do u have any websites to teach well on XML and C#? THanks

Thank you very much.

Jayce
 
Old October 7th, 2003, 02:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I am using MS Visual Studio .NET, which has got a quite extensive documentation, and actually quite good. I also started in a simple text editor in order to get knowledge about all the .NET files. At first glance MS VS .NET makes a lot of files, which you havn't told it to do explicitly, and I wanted to know what these files represented... however I have swiched to MS VS .NET.

I have got a book, which has got some examples, and a reference part, however you can benefit from looking in the MSDN, e.g. here.http://msdn.microsoft.com/library/de...fsystemxml.asp

Jacob.
 
Old October 7th, 2003, 11:51 PM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Jacob,

Thanks for your information.

Now, I am trying the datagrid with XML for my example that I posted here. I am using the AutoGeneratedColumns = true and together with the datagrid's edit/delete/update Commands.

The problem now is that when I click EDIT, the columns which displaying the text of the elements(eg: 123456) will be displayed in a textbox under the ISBN Column. After when I have modified the value, I have to click UPDATE in order to update this value into the xml appropriately. But, how am I going to update it since i use autogeneratedcolumns (coz my xml will contain dynamic info), where i do not know about the textbox name and cannot specify which row in the xml to be updated.

For instance, the solution for autogeneratedcolumns=false will be as following, where the textbox name for editing is being known and the row in xml to be modified is known too:

int row = Convert.ToInt32(e.Item.ItemIndex);
TextBox edittext = null;
edittext = (TextBox)e.Item.FindControl("txtshortdesc");
dataset.Tables[0].Rows[row]["ShortDesc"] = edittext.Text;

So, how am i going to convert this part of programming to meet my requirements? i.e. without knowing which control to find, and the name of the element in the xml, so that the text of it can be modified.

Thanks

p/s: Here is the previous Xml file:

<?xml version="1.0" standalone="yes"?>
<Items>
    <Books>
        <ISBN>123456</ISBN>
        <Title>ASP.NET</Title>
        <Publisher>Wrox</Publisher>
    </Books>
    <DVD>
        <Title>Princess Diary</Title>
        <Region>Asia</Region>
    </DVD>
    <Games>
        <Title>Super Mario</Title>
        <Platform>PC</Platform>
    </Games>
</Items>

Thank you very much

Regards
Jayce
 
Old October 8th, 2003, 02:26 AM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

   How to add additional child elements into an XML file? like the following example:

<?xml version="1.0" standalone="yes"?>
<Items>
    <Books>
        <ISBN>123456</ISBN>
        <Title>ASP.NET</Title>
        <Publisher>Wrox</Publisher>
    </Books>
    <DVD>
        <Title>Princess Diary</Title>
        <Region>Asia</Region>
    </DVD>
    <Games>
        <Title>Super Mario</Title>
        <Platform>PC</Platform>
    </Games>

    <New Element>
        <ABC> XXX </ABC>
        <XYZ> AAA </XYZ>
    </New Element>
</Items>


Thank you very much.

Regards,
Jayce :(





Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening XML. Its too urgent dhara_adh XML 2 January 29th, 2007 05:27 AM
Xml to XML using XSLT (urgent) [email protected] XSLT 1 December 30th, 2005 01:48 PM
Urgent help regarding XSLT parsing.... to xml.. netbramha XSLT 1 September 19th, 2005 09:03 AM
Rearranging XML - Urgent. rmiller XSLT 2 October 6th, 2004 03:08 PM
urgent need for open an XML with word2003 AyatKh Classic ASP XML 2 December 30th, 2003 01:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.