update/delete/insert nodes to an xml document
Load an xml document to memory. Nevigate the DOM object, using xpath to query nodes. To update the node: create an new one, and replace the existing one.
Default namespace in xml doc still needs to declare in namespace mamager- and use in the xpath query. Although it is not present in the xml data:
Sample Data
C# Code to do this:
private void SetTestData(int index, int itemsPerPage, int totalResults)
{
XmlTextReader reader = new XmlTextReader("..\\TestData_V1.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
string osUri = "http://a9.com/-/spec/opensearch/1.1/";
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("os", osUri);
nsMgr.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nsMgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
nsMgr.AddNamespace("rss", "http://purl.org/rss/1.0/");
XmlNode oStartIndex, oItermsPerPage, oTotalResults;
XmlElement root = doc.DocumentElement;
oStartIndex = root.SelectSingleNode("/rdf:RDF/rss:channel/os:totalResults", nsMgr);
oItermsPerPage = root.SelectSingleNode("/rdf:RDF/rss:channel/os:itemsPerPage", nsMgr);
oTotalResults = root.SelectSingleNode("/rdf:RDF/rss:channel/os:startIndex", nsMgr);
XmlElement nStartIndex = doc.CreateElement("os", "startIndex", osUri);
XmlElement nItermsPerPage = doc.CreateElement("os", "itemsPerPage", osUri);
XmlElement nTotalResults = doc.CreateElement("os", "totalResults", osUri);
nStartIndex.InnerXml = index.ToString();
nItermsPerPage.InnerXml = itemsPerPage.ToString();
nTotalResults.InnerXml = totalResults.ToString();
root.SelectSingleNode("/rdf:RDF/rss:channel", nsMgr).ReplaceChild(nStartIndex, oStartIndex);
root.SelectSingleNode("/rdf:RDF/rss:channel", nsMgr).ReplaceChild(nItermsPerPage, oItermsPerPage);
root.SelectSingleNode("/rdf:RDF/rss:channel", nsMgr).ReplaceChild(nTotalResults, oTotalResults);
doc.Save("..\\tempTestData.xml");
}
private void SetTestData(int index, int itemsPerPage, int totalResults)
{
XmlTextReader reader = new XmlTextReader("..\\TestData_V1.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
string osUri = "http://a9.com/-/spec/opensearch/1.1/";
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("os", osUri);
nsMgr.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nsMgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
nsMgr.AddNamespace("rss", "http://purl.org/rss/1.0/");
XmlNode oStartIndex, oItermsPerPage, oTotalResults;
XmlElement root = doc.DocumentElement;
oStartIndex = root.SelectSingleNode("/rdf:RDF/rss:channel/os:totalResults", nsMgr);
oItermsPerPage = root.SelectSingleNode("/rdf:RDF/rss:channel/os:itemsPerPage", nsMgr);
oTotalResults = root.SelectSingleNode("/rdf:RDF/rss:channel/os:startIndex", nsMgr);
XmlElement nStartIndex = doc.CreateElement("os", "startIndex", osUri);
XmlElement nItermsPerPage = doc.CreateElement("os", "itemsPerPage", osUri);
XmlElement nTotalResults = doc.CreateElement("os", "totalResults", osUri);
nStartIndex.InnerXml = index.ToString();
nItermsPerPage.InnerXml = itemsPerPage.ToString();
nTotalResults.InnerXml = totalResults.ToString();
root.SelectSingleNode("/rdf:RDF/rss:channel", nsMgr).ReplaceChild(nStartIndex, oStartIndex);
root.SelectSingleNode("/rdf:RDF/rss:channel", nsMgr).ReplaceChild(nItermsPerPage, oItermsPerPage);
root.SelectSingleNode("/rdf:RDF/rss:channel", nsMgr).ReplaceChild(nTotalResults, oTotalResults);
doc.Save("..\\tempTestData.xml");
}