Searching a particular node in XML
-
... .. .. If , we have to get the node corresponding to the Answers node with id = 1 , how can we do that? Shankar C
-
... .. .. If , we have to get the node corresponding to the Answers node with id = 1 , how can we do that? Shankar C
A good idea would be to load the contents of your XML file into a dataSet and do your searching on the dataSet instead. This gives you the advantage of handling XML data as if it was an ordinary Database. Regards, Polis Can you practice what you teach?
-
A good idea would be to load the contents of your XML file into a dataSet and do your searching on the dataSet instead. This gives you the advantage of handling XML data as if it was an ordinary Database. Regards, Polis Can you practice what you teach?
private void ImportXML(string path) { XmlTextReader reader = null; try { reader = new XmlTextReader(Server.MapPath("../CheckList/"+strFileName)); } catch { Response.Write("File Does'nt exists"); } XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlDocument docNew = new XmlDocument(); docNew.Load(path); XmlNode oldCd = null; bool Append = false; XmlElement root = doc.DocumentElement; oldCd = root.SelectSingleNode("//answers[@objectid=" + intValue + "]"); if(oldCd == null) Append = true; XmlNode CdNew; XmlElement rootNew = docNew.DocumentElement; CdNew = rootNew.SelectSingleNode("/answers[@objectid=" + intValue + "]"); XmlNode newBook = doc.ImportNode(CdNew, true); XmlNode newNode = doc.DocumentElement.ChildNodes[1]; if(Append) newNode.AppendChild(newBook); else newNode.ReplaceChild(newBook,oldCd); try { doc.Save(Server.MapPath("../CheckList/"+strFileName)); } catch { string strPath = Server.MapPath("../CheckList/") + "TempFile.xml"; ImportXML(strPath); } } can u help me in another way to do this?
-
private void ImportXML(string path) { XmlTextReader reader = null; try { reader = new XmlTextReader(Server.MapPath("../CheckList/"+strFileName)); } catch { Response.Write("File Does'nt exists"); } XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlDocument docNew = new XmlDocument(); docNew.Load(path); XmlNode oldCd = null; bool Append = false; XmlElement root = doc.DocumentElement; oldCd = root.SelectSingleNode("//answers[@objectid=" + intValue + "]"); if(oldCd == null) Append = true; XmlNode CdNew; XmlElement rootNew = docNew.DocumentElement; CdNew = rootNew.SelectSingleNode("/answers[@objectid=" + intValue + "]"); XmlNode newBook = doc.ImportNode(CdNew, true); XmlNode newNode = doc.DocumentElement.ChildNodes[1]; if(Append) newNode.AppendChild(newBook); else newNode.ReplaceChild(newBook,oldCd); try { doc.Save(Server.MapPath("../CheckList/"+strFileName)); } catch { string strPath = Server.MapPath("../CheckList/") + "TempFile.xml"; ImportXML(strPath); } } can u help me in another way to do this?
Load data from the XML document into a dataSet:
ds
Create a new row based on the table of the dataSet
DataRow
drs
= ds.Tables["Table1"].NewRow();
//Construct field values for the new row
drs["Message"] = "Hello World!"
//Add the new row into the dataTable
ds.Tables[table].Rows.Add(drs);Delete an existing row from the table
if (ds.Tables["Table1"].Rows.Count > 0)
{
//Remove the selected row from the dataSet (assuming we use a dataGrid)
ds.Tables[table].Rows.RemoveAt(this.dataGrid1.CurrentCell.RowNumber);
}Search for a value
if (ds.Tables["Table1"].Rows.Count == 1)
if (ds.Tables["Table1"].Rows[0][ds.Tables["Table1"].Columns[0]].ToString() == "Hello World!")
MessageBox.Show("Found");Save the changes to the XML file
ds.WriteXml(fileName);
Hope this will help you get the picture. Regards, Polis Can you practice what you teach?
-
Load data from the XML document into a dataSet:
ds
Create a new row based on the table of the dataSet
DataRow
drs
= ds.Tables["Table1"].NewRow();
//Construct field values for the new row
drs["Message"] = "Hello World!"
//Add the new row into the dataTable
ds.Tables[table].Rows.Add(drs);Delete an existing row from the table
if (ds.Tables["Table1"].Rows.Count > 0)
{
//Remove the selected row from the dataSet (assuming we use a dataGrid)
ds.Tables[table].Rows.RemoveAt(this.dataGrid1.CurrentCell.RowNumber);
}Search for a value
if (ds.Tables["Table1"].Rows.Count == 1)
if (ds.Tables["Table1"].Rows[0][ds.Tables["Table1"].Columns[0]].ToString() == "Hello World!")
MessageBox.Show("Found");Save the changes to the XML file
ds.WriteXml(fileName);
Hope this will help you get the picture. Regards, Polis Can you practice what you teach?
Thanx polis, but can u tell about the ImportNodes method in the XML api?
-
Thanx polis, but can u tell about the ImportNodes method in the XML api?
ImportNodes method? Hmm... I am not aware of this one Shankar, sorry. XML API? What exactly do you mean by this? Regards, Polis Can you practice what you teach?
-
ImportNodes method? Hmm... I am not aware of this one Shankar, sorry. XML API? What exactly do you mean by this? Regards, Polis Can you practice what you teach?
In the System.XML namespace , we have a class called XMLDocument. If we create an object for the above said class, we can call the ImportNodes method. It's working correctly. but i need to know the correct usage for importnodes method.
-
In the System.XML namespace , we have a class called XMLDocument. If we create an object for the above said class, we can call the ImportNodes method. It's working correctly. but i need to know the correct usage for importnodes method.
Oh right. I just had a quick read on the MSDN[^]about the ImportNode() method. Well, it seems that all it does is to import specified XML nodes from another XML file into the one you are working with. E.g. XML File #1 (Current XML file you work with) Nodes: FirstName, LastName, Age XML File #2 Nodes: FirstName, LastName, Age, Address By saying something like "....ImportNode(doc2.DocumentElement.LastChild, true);", it will append the Address node into your XML file #1. Regards, Polis Can you practice what you teach?
-
... .. .. If , we have to get the node corresponding to the Answers node with id = 1 , how can we do that? Shankar C
My article Accessing XML using insert/update/delete/query statements[^] might be able to help--it should be really easy! Marc MyXaml Advanced Unit Testing YAPO