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 October 8th, 2003, 03:56 AM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

  I tried with the following but it doesn't return the results that I wanted:

//create a new node and add it to the document.
XmlElement newelement = doc.CreateElement("Testing");
doc.DocumentElement.AppendChild(newelement);
XmlNode newElem = doc.CreateNode(XmlNodeType.Element, "pages", "");
newElem.InnerText = "290";
XmlElement root = doc.DocumentElement;
root.AppendChild(newElem);
doc.Save(sourceXml2);

The results:
<Items>
    <Testing />
    <pages>290</pages>
</Items>

where it should return this:
<Items>
    <Testing>
      <pages>290</pages>
    </Testing>
</Items>

So, may I know whether if you have any solutions for this?

Thanks

Regards,
Jayce
 
Old October 8th, 2003, 04:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK! That was a lot !

I havn't worked with datagrids at all so I cannot answer that, however perhaps I should try it, so that I can get some experience with this.

About the insertion of the new composite construction in the XML file I can help... I have worked with the previous example, and it turned out like this...
Code:
public void Page_Load(Object oSender, EventArgs e)
{
    String filename = Server.MapPath("books.xml");

    XmlDocument doc = new XmlDocument();
    XmlElement[] elements = new XmlElement[3];
    try
    {
        doc.Load(filename);
        elements[0] = doc.CreateElement("NewElement");

        elements[1] = doc.CreateElement("ABC");
        elements[1].AppendChild(doc.CreateTextNode("XXX"));
        elements[2] = doc.CreateElement("XYZ");
        elements[2].AppendChild(doc.CreateTextNode("AAA"));

        elements[0].AppendChild(elements[1]);
        elements[0].AppendChild(elements[2]);

        (doc.DocumentElement).AppendChild(elements[0]);

        doc.Save(filename);        
    }
    catch(Exception exeption)
    {

    }
}
I have tried it, and it should work. I will let you know it I get to try the datagrid.

Hope it will help

Jacob.


 
Old October 10th, 2003, 01:14 AM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

  Your solution is awesome! Thanks alot!!! ;)
It took me some time to fully understand it. Thank you.

But, What if I wanna read the Elements names from my xml?
<?xml version="1.0" standalone="yes"?>
<Items>
    <Books>
        <ISBN>123456</ISBN>
        <Title>ASP.NET</Title>
        <Publisher>Wrox</Publisher>
    </Books>
</Items>

for instance, i wanna read the elements of <Books> i.e. ISBN, Title, and Publisher.
I tried with :

XmlNodeList nodelist;
nodelist = root.GetElementsByTagName("Books");
IEnumerator ienum = nodelist.GetEnumerator();
while (ienum.MoveNext())
{
   XmlNode nodes = (XmlNode) ienum.Current;
   msg.Text += nodes.InnerText;
}

but, it returns the values of each elements like 123456, ASP.net, Wrox.

Thus, may I know the correct way in doing it?

Thank you

Jayce
 
Old October 10th, 2003, 01:43 AM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

  Let say I have the following XML:

<?xml version="1.0" standalone="yes"?>
<Items>
    <Books>
        <ISBN>123456</ISBN>
        <Title>ASP.NET</Title>
        <Publisher>Wrox</Publisher>
    </Books>
    <Books>
        <ISBN></ISBN>
        <Title></Title>
        <Publisher></Publisher>
    </Books>
</Items>

and now I wanted to insert/ modify values into the second part of <Books> where its elements contain no values. How am I going to detect which <Books> is without text in its child elements and then insert the new data into it?

Thank you

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.