Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 7th, 2007, 08:58 AM
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to append child node after an node in XML + C#

Hello every
I need a help for writing xml elements. Can any one help me how we repeat a whole chile node with attributes, how can I repeat <FieldToSupport> whole block After the </SupportItems> block

---------------------------------------------------------
This is my XML file structure

<?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>
      <ScreenUniqueId>c1f13323-209b-4b0e-ad8d-d34f64d34c28</ScreenUniqueId>
<SupportItems>
        <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\iframe.html</LinkURL>
        <LinkText>New Link Help</LinkText>
        <ScreenSupportItem>
          <LinkDate>2007-10-25T11:47:10.515625+05:30</LinkDate>
          <LinkAuthor>SUBHASH\Login Subhash</LinkAuthor>
          <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\main.html</LinkURL>
          <LinkText>Adding New Html Help</LinkText>
          <OptionalSupportInstruction />
          <RefId>3</RefId>
        </ScreenSupportItem>
</SupportItems>
<FieldsToSupport>
        <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\iframe.html</LinkURL>
        <LinkText>New Link Help</LinkText>
        <FieldToSupport>
          <Caption>Blank</Caption>
          <Url>Blank</Url>
          <Label>NewField1</Label>
          <HtmlName />
          <HtmlId />
          <FieldUniqueId>15b85c2b-83ca-44ec-8741-22a4dc64f64d</FieldUniqueId>
          <LinkHref>http://careers.msn.com/</LinkHref>
          <InnerHTML>Careers &amp;amp; Jobs</InnerHTML>
          <OuterHTML>&lt;A href="http://careers.msn.com"&gt;Careers &amp;amp; Jobs&lt;/A&gt;</OuterHTML>
          <PopupHTML />
          <FieldSupportItems />
        </FieldToSupport>
<FieldToSupport>
..
</FieldToSupport>
<FieldToSupport>
..
</FieldToSupport>
</FieldsToSupport>
      <Label>label3</Label>
      <ScreenMatchProbabilityLevel>172.0934</ScreenMatchProbabilityLevel>
    </ScreenToSupport>
  </ScreensToSupport>
</LinkInformation>
---------------------------------------------------------
This is my sample code in which I repeat the <LinkURl> and <LinkText> field.
but I have to now repeat whole <SupportItems> tags
How can I did this pls any one can response me. I will very thankful for that.
---------------------------------------------------------
This is my sample code


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);

                foreach (System.Xml.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)
                {
                    lblError.Text = "LinkURL/LinkText already exist !";
                    BaydonConstants.flag = false;
                }
                else
                {
                    XmlNode newSibling = newParent["LinkAuthor"];
                    XmlElement newLinkURL = doc.CreateElement("LinkURL");
                    XmlElement newLinkText = doc.CreateElement("LinkText");
                    newLinkText.InnerText = txtUrlTitle.Text;
                    newLinkURL.InnerText = FileUpload.PostedFile.FileName;
                    newParent.InsertAfter(newLinkURL, newSibling);
                    newParent.InsertAfter(newLinkText, newLinkURL);
                    doc.Save(strXMLFile);
                    lblError.Text = "Inserted Successfully !";
                }
            }
        }
-----------------------------------------------------------


Reply With Quote
  #2 (permalink)  
Old November 7th, 2007, 09:28 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

To create a copy of a node you can use the XmlNode.Clone() method to create the copy.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old November 8th, 2007, 01:57 AM
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello sir I have tried it but its not working according to my requirement. I have to create a new child <ScreenSupportItem> below the parent node <SupportItems> with matching condition --where attribute of <ScreenSupportItem> e.g <RefId> is equal to 3 (comes from query string). Also new <ScreenSupportItem> child have different attributes except <RefId>. So I have to create a <ScreenSupportItem> with setting new values of all the attributes.

This is the structure of 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>
    ........all have same structure <ScreenToSupport>
    </ScreenToSupport>
    <ScreenToSupport>
    ........all have same structure <ScreenToSupport>
    </ScreenToSupport>
    <ScreenToSupport>
      <ScreenUniqueId>c1f13323-209b-4b0e-ad8d-d34f64d34c28</ScreenUniqueId>
<SupportItems>
        <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\iframe.html</LinkURL>
        <LinkText>New Link Help</LinkText>
        <ScreenSupportItem>
          <LinkDate>2007-10-25T11:47:10.515625+05:30</LinkDate>
          <LinkAuthor>SUBHASH\Login Subhash</LinkAuthor>
          <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\main.html</LinkURL>
          <LinkText>Adding New Html Help</LinkText>
          <OptionalSupportInstruction />
          <RefId>3</RefId>
        </ScreenSupportItem>
</SupportItems>
<FieldsToSupport>
        <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\iframe.html</LinkURL>
        <LinkText>New Link Help</LinkText>
        <FieldToSupport>
          <Caption>Blank</Caption>
          <Url>Blank</Url>
          <Label>NewField1</Label>
          <HtmlName />
          <HtmlId />
          <FieldUniqueId>15b85c2b-83ca-44ec-8741-22a4dc64f64d</FieldUniqueId>
          <LinkHref>http://careers.msn.com/</LinkHref>
          <InnerHTML>Careers &amp;amp; Jobs</InnerHTML>
          <OuterHTML>&lt;A href="http://careers.msn.com"&gt;Careers &amp;amp; Jobs&lt;/A&gt;</OuterHTML>
          <PopupHTML />
          <FieldSupportItems />
        </FieldToSupport>
<FieldToSupport>
..
</FieldToSupport>
<FieldToSupport>
..
</FieldToSupport>
</FieldsToSupport>
      <Label>label3</Label>
      <ScreenMatchProbabilityLevel>172.0934</ScreenMatchProbabilityLevel>
        </ScreenToSupport>
    <ScreenToSupport>
    ........all have same structure <ScreenToSupport>
    </ScreenToSupport>
  </ScreensToSupport>
</LinkInformation>
-----------------------------------------------------------------
And this is my sample code
-----------------------------------------------------------------
strXMLFile = strAlbumPath + "\\" + strAlbumName + ".linkinfo.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(strXMLFile);
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;

                XmlNode newRootParent = doc.SelectSingleNode(strXPath).ParentNode;

                XmlNode newScreenToSupport = newParent.Clone();
                newRootParent.InsertAfter(newRootParent, newScreenToSupport);
                doc.Save(strXMLFile);
-----------------------------------------------------------------
It giving error 'ERROR:Cannot insert a node or any ancestor of that node as a child of itself.'
I am very new in xml please help me to solved my problem. I will very thankful for your valuable cooporation.
Please reply me soon....

Reply With Quote
  #4 (permalink)  
Old November 9th, 2007, 05:24 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well for starters you've got the parameters the wrong way around for the InsertAfter method. Personally I'd just use the AppendChild method.

XmlNode newScreenToSupport = newParent.Clone();
newParent.ParentNode.AppendChild(newScreenToSuppor t);

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old November 13th, 2007, 05:18 AM
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);
                }
            }
        }
----------------------------------------------------------------

Reply With Quote
  #6 (permalink)  
Old November 13th, 2007, 05:30 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Please don't ask the same question in two places - it then forces people to take twice as long to work out what you want.

http://p2p.wrox.com/topic.asp?TOPIC_ID=66713

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
append a xml node from one file to another pravind XSLT 5 August 22nd, 2009 09:15 AM
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
How to count the child node suri_1811 XSLT 6 November 10th, 2006 04:51 PM
Child Node Output Neal XSLT 0 June 7th, 2006 03:14 AM
delete XML node from JavaScript - not child crmpicco Javascript How-To 1 April 27th, 2006 11:42 AM



All times are GMT -4. The time now is 05:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.