View Single Post
 
Old November 13th, 2007, 05:18 AM
vishnu108mishra vishnu108mishra is offline
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello sir

There is a little problem of xml again.
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);
                }
            }
        }
----------------------------------------------------------------