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

November 7th, 2007, 08:58 AM
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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; Jobs</InnerHTML>
<OuterHTML><A href="http://careers.msn.com">Careers &amp; Jobs</A></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 !";
}
}
}
-----------------------------------------------------------
|

November 7th, 2007, 09:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
To create a copy of a node you can use the XmlNode.Clone() method to create the copy.
/- Sam Judson : Wrox Technical Editor -/
|

November 8th, 2007, 01:57 AM
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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; Jobs</InnerHTML>
<OuterHTML><A href="http://careers.msn.com">Careers &amp; Jobs</A></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....
|

November 9th, 2007, 05:24 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|

November 13th, 2007, 05:18 AM
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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);
}
}
}
----------------------------------------------------------------
|

November 13th, 2007, 05:30 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|
 |