My XML is given below.
<UrlRoutes>
<ActivityPR>
<Source>activity/editactivity</Source>
<DestinationController>Activity</DestinationController>
<DestinationAction>Editactivity</DestinationAction>
</ActivityPR>
<UserSettings>
<Source>settings/subscriptions</Source>
<DestinationController>UserSettings</DestinationController>
<DestinationAction>GetUserPreferenceSettings</DestinationAction>
</UserSettings>
</UrlRoutes>
I will have Source element value in a var say sourceX . Ex
SourceX = "settings/subscriptions"
or
SourceX = "activity/editactivity"
I am trying to get the parent node using the below code, let me know if there is something wrong
XmlElement xmlNode = xmlDoc.GetElementById(SourceX);
XmlNode parent = xmlNode.ParentNode;
Now for the combination for the parent node(say ActivityPR or Usersettings) and Source, I have to find the corresponding DestinationController and DestinationAction.
How do I do that? Prefer traditional XML as opposed to LINQ as rest of the code is in that form.
if(node!=null)
{
XmlElement routeElement = (XmlElement)node;
strController = routeElement.GetElementsByTagName("DestinationController")[0].InnerText.ToString();
strAction = routeElement.GetElementsByTagName("DestinationAction")[0].InnerText.ToString();
}
I’m assuming you’ve already loaded the XML into your
XmlDocument– correct?In that case, you should be able to use something like this:
The xpath expression basically selects any node under
/UrlRoutesthat contains a<Source>element with the given string as its value. This means: the values of<Source>need to be unique.Update: if you know you want to search only inside nodes
<UserSettings>, you can make your XPath expression more selective:But with this more selective XPath, you would not be able to find a node when you have
sourceX = "activity/editactivity"in your example (since that is not inside a<UserSettings>node)Update #2: I would probably use this code to grab the elements inside your node:
That way, you don’t need to convert to an
XmlElementfirst.