Knowledge Base Nr: 00285 xpathsearch.cs - http://www.swe-kaiser.de

Downloads:

c#: in xml suchen mit XPath (verwendet namespaces)

  
XPathDocument doc = new XPathDocument(@"..\..\XmlFiles\kombi.xml");

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

XPathExpression query = nav.Compile("//ho:SHORT-NAME");
query.SetContext(man);

XPathNodeIterator iter = nav.Select(query); //"/FIBEX/ELEMENTS/ECUS/ECU[@ID='component1']");

while (iter.MoveNext())
{
string id = iter.Current.GetAttribute("ID", "");
string str1 = iter.Current.LocalName + " " + iter.Current.LocalName +" " + id;
System.Console.WriteLine(str1);

//alle untergeordneten knoten
//XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element,true);

//alle übergeordneten knoten
XPathNodeIterator newIter = iter.Current.SelectAncestors(XPathNodeType.Element,true);
while (newIter.MoveNext())
{
string str = newIter.Current.Name + " " + newIter.Current.Value;
}
}

////////////////
short-name (element)
non exact match
non case sensitive
XPathExpression query = nav.Compile("//ho:SHORT-NAME");
while (iter.MoveNext())
{
if (iter.Current.Value == search)
{
}
}
////////////////
ids (property)
exact match
case sensitive
XPathExpression query = nav.Compile("//*[@ID='bsg']"); <<<<<search
while (iter.MoveNext())
{
string str1 = iter.Current.Name + " " + iter.Current.Value;
}

////////////////
rechte seite (elemente)
exact match
case sensitive
XPathExpression query = nav.Compile("//*[*='lullibertproto']");
query.SetContext(man);

XPathNodeIterator iter = nav.Select(query); //"/FIBEX/ELEMENTS/ECUS/ECU[@ID='component1']");

while (iter.MoveNext())
{
string str1 = iter.Current.Name + " " + iter.Current.Value;
}
////////////////
////////////////