Knowledge Base Nr: 00291 xmlxpath.cs - http://www.swe-kaiser.de
Downloads:
.net: xml: c#: search xml nodes with xpath
public void SearchNodes(string searchString, bool bCaseSensitive, bool bExactMatch, bool bSearchTree, bool bSearchProperties)
{
this.Cursor = Cursors.WaitCursor;
if (lastFileName == "")
return;
//save changes before search?
if (configurationChanged == true)
{
DialogResult res = MessageBox.Show("You have made changes. Do you want to save before searching?", "Save Changes", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
{
SaveFibex(lastFileName);
}
}
XPathDocument doc = new XPathDocument(lastFileName);
XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
XmlNamespaceManager man = new XmlNamespaceManager(nav.NameTable);
man.AddNamespace("fx", "http://www.asam.net/xml/fbx");
man.AddNamespace("flexray", "http://www.asam.net/xml/fbx/flexray");
man.AddNamespace("lin", "http://www.asam.net/xml/fbx/lin");
man.AddNamespace("can", "http://www.asam.net/xml/fbx/can");
man.AddNamespace("ho", "http://www.asam.net/xml");
man.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
man.AddNamespace("xs", "http://www.asam.net/xml/fbx/all http://www.asam.net/xml/fbx/1_2_0a/xsd/fibex4multiplatform.xsd");
searchResultsDlg.InitSearchResult();
if (bSearchTree)
{
XPathExpression query = nav.Compile("//*[@ID|@ID-REF]");
query.SetContext(man);
XPathNodeIterator iter = nav.Select(query);
bool bIsSameID = false;
bool bIsSameIDRef = false;
string elementid = "";
string lastelementid = "";
while (iter.MoveNext())
{
if (bCaseSensitive)
{
if (bExactMatch)
{
bIsSameID = (iter.Current.GetAttribute("ID", "") == searchString);
bIsSameIDRef = (iter.Current.GetAttribute("ID-REF", "") == searchString);
}
else
{
bIsSameID = (iter.Current.GetAttribute("ID", "").IndexOf(searchString)) >= 0;
bIsSameIDRef = (iter.Current.GetAttribute("ID-REF", "").IndexOf(searchString)) >= 0;
}
}
else
{
if (bExactMatch)
{
bIsSameID = (iter.Current.GetAttribute("ID", "").ToLower() == searchString.ToLower());
bIsSameIDRef = (iter.Current.GetAttribute("ID-REF", "").ToLower() == searchString.ToLower());
}
else
{
bIsSameID = (iter.Current.GetAttribute("ID", "").ToLower().IndexOf(searchString.ToLower())) >= 0;
bIsSameIDRef = (iter.Current.GetAttribute("ID-REF", "").ToLower().IndexOf(searchString.ToLower())) >= 0;
}
}
if (bIsSameID || bIsSameIDRef)
{
SearchNode nodeInfo = new SearchNode();
XPathNodeIterator newIter = iter.Current.SelectAncestors(XPathNodeType.Element, true);
bool bFirst = true;
while (newIter.MoveNext())
{
string elementName = newIter.Current.LocalName;
nodeInfo.arPath.Insert(0, elementName);
elementid = newIter.Current.GetAttribute("ID", "");
nodeInfo.arInfo.Insert(0, elementid);
if (elementid == "")
elementid = lastelementid;
nodeInfo.arIds.Insert(0, elementid);
lastelementid = elementid;
if (bFirst)
{
nodeInfo.search = searchString;
nodeInfo.bIsIDREF = bIsSameIDRef;
bFirst = false;
}
}
searchResultsDlg.AddSearchResult(this, nodeInfo);
}
}
}
if (bSearchProperties)
{
XPathExpression query;
if (bCaseSensitive)
{
if (bExactMatch)
query = nav.Compile("//*[*='" + searchString + "']");
else
query = nav.Compile("//*[contains(*,'" + searchString + "')]");
}
else
{
if (bExactMatch)
query = nav.Compile("//*[translate(*, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='" + searchString.ToLower() + "']");
else
query = nav.Compile("//*[contains(translate(*, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'" + searchString.ToLower() + "')]");
}
query.SetContext(man);
XPathNodeIterator iter = nav.Select(query);
string lastelementid = "";
while (iter.MoveNext())
{
SearchNode nodeInfo = new SearchNode();
XPathNodeIterator newIter = iter.Current.SelectAncestors(XPathNodeType.Element, true);
bool bFirst = true;
while (newIter.MoveNext())
{
string elementName = newIter.Current.LocalName;
nodeInfo.arPath.Insert(0, elementName);
string elementid = newIter.Current.GetAttribute("ID", "");
nodeInfo.arInfo.Insert(0, elementid);
if (elementid == "")
elementid = lastelementid;
nodeInfo.arIds.Insert(0, elementid);
lastelementid = elementid;
if (bFirst)
{
nodeInfo.search = elementid;
nodeInfo.bIsIDREF = false;
bFirst = false;
}
}
searchResultsDlg.AddSearchResult(this, nodeInfo);
}
}
this.Cursor = Cursors.Default;
}