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 November 13th, 2007, 04:30 AM
Authorized User
 
Join Date: Nov 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default How can I go through all child nodes & attribute..

Hello every one..

There is a little problem of xml ..??
How can I go through all child nodes and attributes of child nodes of a parent node..

Suppose i have to go through all child nodes e.g.<ScreenSupportItem> of parent nodes

e.g.<SupportItems>. There can be mutiple <ScreenSupportItem> in a parent node <SupportItems>. I have

to check values of <LinkURL> and <LinkText> of all child nodes <ScreenSupportItem> and if it matches

with the supplied valus it gives a message 'LinkUrl/LinkText already exist' else insert new

<ScreenSupportItem> of <ScreenSupportItem> and set all attributes of <ScreenSupportItem> , which was

supplied by the user.
My code check only first child <ScreenSupportItem> of <SupportItems> if it values matches for

<LinkURL> and <LinkText> give a proper message else insert new <ScreenSupportItem>.
So how can I check all child <ScreenSupportItem> for values <LinkURL> and <LinkText> of

<SupportItems>

This is a sample struture of my XML file
----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinkInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LinkToolId>0</LinkToolId>
  <LinkToolVersion>1.0.1</LinkToolVersion>

    <ScreensToSupport>
        <ScreenToSupport>
          <LinkDate>2007-11-13T12:48:29.9687500+05:30</LinkDate>
              <LinkAuthor>SUBHASH\vishnu</LinkAuthor>
              <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\iframe.html</LinkURL>
              <LinkText>HTML Help File</LinkText>
              <OptionalSupportInstruction />
              <RefId>3</RefId>
        </ScreenToSupport>

        <ScreenToSupport>
        <LinkDate>2007-11-13T13:04:40.2656250+05:30</LinkDate>
              <LinkAuthor>SUBHASH\vishnu</LinkAuthor>
            <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\main.html</LinkURL>
            <LinkText>Adding new Help Link</LinkText>
            <OptionalSupportInstruction />
            <RefId>3</RefId>
        </ScreensToSupport>

        <ScreenToSupport>
        <LinkDate>2007-11-13T13:04:24.8437500+05:30</LinkDate>
            <LinkAuthor>SUBHASH\vishnu</LinkAuthor>
            <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\iframeCopy.html</LinkURL>
            <LinkText>New Link Help</LinkText>
            <OptionalSupportInstruction />
            <RefId>3</RefId>
        </ScreensToSupport>

        <ScreenToSupport>
        .....
       </ScreenToSupport>

       </ScreensToSupport>

  </LinkInformation>
----------------------------------------------------------------


follwing is my code..
please reply me soon sir .. i will very thankful for that..
----------------------------------------------------------------
if (strAlbumName != "" && strAlbumPath != "")
            {
                strXMLFile = strAlbumPath + "\\" + strAlbumName + ".linkinfo.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(strXMLFile);

                System.Data.DataSet objDataSet = new DataSet();

                FileStream findata = new FileStream(strXMLFile, FileMode.Open, FileAccess.Read,

FileShare.ReadWrite);
                objDataSet.ReadXml(findata);
                findata.Close();
                //int intRefId = Convert.ToInt32(Request.QueryString["RefId"]);
                int intRefId = 3;
                string strFilePath = FileUpload.PostedFile.FileName;
                string strXPath =

"/LinkInformation/ScreensToSupport/ScreenToSupport/SupportItems/ScreenSupportItem[RefId='" +

intRefId + "']";

                XmlNode newParent =

doc.SelectSingleNode(strXPath);//.ParentNode.NextSibling.ChildNodes;

                foreach (XmlNode node in newParent)
                {
                    if (node.Name == "LinkURL")
                    {
                        if (node.InnerText == FileUpload.PostedFile.FileName)
                        {
                            BaydonConstants.flag = true;
                            break;
                        }
                    }
                    else if (node.Name == "LinkText")
                    {
                        if (node.InnerText == txtUrlTitle.Text)
                        {
                            BaydonConstants.flag = true;
                            break;
                        }
                    }
                }
                if (BaydonConstants.flag == true)
                {
                    lblError.Text = "LinkUrl/LinkText already exist !";
                    BaydonConstants.flag = false;
                }
                else
                {
                    string strMachineName = System.Environment.MachineName + "\\";
                    XmlNode newRootParent = doc.SelectSingleNode(strXPath).ParentNode;
                    int intNodeCount = newRootParent.ChildNodes.Count;
                    if (intNodeCount == 1 && (newParent["LinkURL"].InnerText.ToString() == "" &&

newParent["LinkText"].InnerText.ToString() == ""))
                    {
                        newParent["LinkDate"].InnerText = DateTime.Now.ToString("o");
                        newParent["LinkAuthor"].InnerText = strMachineName +

Session["UserId"].ToString();
                        newParent["LinkURL"].InnerText = FileUpload.PostedFile.FileName;
                        newParent["LinkText"].InnerText = txtUrlTitle.Text;
                        lblError.Text = "Inserted successfully !";
                    }
                    else
                    {
                        XmlNode newScreenToSupport = newParent.Clone();
                        newScreenToSupport["LinkDate"].InnerText = DateTime.Now.ToString("o");
                        newScreenToSupport["LinkAuthor"].InnerText = strMachineName +

Session["UserId"].ToString();
                        newScreenToSupport["LinkURL"].InnerText = FileUpload.PostedFile.FileName;
                        newScreenToSupport["LinkText"].InnerText = txtUrlTitle.Text;

                        newRootParent.InsertBefore(newScreenToSupport, newParent);
                        lblError.Text = "Inserted successfully !";
                    }
                    doc.Save(strXMLFile);
                }
            }
        }
----------------------------------------------------------------

 
Old November 13th, 2007, 05:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It sounds like you need to get a book, or find some basic tutorials on C# and Xml. But because I'm kind here's some pointers that I hope will help.

http://msdn2.microsoft.com/en-us/lib...e_members.aspx

Every element in the XML tree inherits from XmlNode. There are various methods that can help you do what you want.

ChildNodes property. Loop through each XmlNode in the ChildNodes property. e.g.

foreach(XmlNode node in parentNode.ChildNodes)
{
  // Do something
}

SelectNodes method. Much like ChildNodes this returns an array of XmlNode's, but based on an xpath expression.

foreach(XmlNode node in parentNode.SelectNodes("SupportItems/ScreenSupportItem"))
{
  // Loop through every ScreenSupportItem.
}

For getting the attributes of a node you can use the Attributes collection

foreach(XmlNode att in parentNode.Attributes)
{
  // att.Value and att.Name give you the attribute details.
}

Or you can use the GetNamedItem method, which returns null if the attribute doesn't exist.

string attValue = parentNode.Attributes.GetNamedItem("LinkURL");
if( attValue == null )
{
}
else
{
}

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
text and child nodes eepyoga XSLT 1 April 8th, 2008 12:45 PM
How can I display parent & child nodes if XML .... vishnu108mishra ASP.NET 2.0 Basics 0 November 17th, 2007 07:45 AM
Retrieveal of child nodes.... nathilson21 Classic ASP XML 0 May 7th, 2007 04:54 AM
Trying to group child nodes aalbetski XSLT 3 November 18th, 2006 01:29 PM





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