0

I have a sample xml

<UserSettings>
   <Source>settings/subscriptions</Source>
   <DestinationController>UserSettings</DestinationController>
   <DestinationAction>GetUserPreferenceSettings</DestinationAction>
</UserSettings>

The reading of the XML using tag name(UserSettings) is done as shown below.

XmlDataDocument xmlDoc = new XmlDataDocument();
strFileName = System.Configuration.ConfigurationManager.AppSettings["UrlRoutingPath"].ToString();
strFileLocation = HttpContext.Current.Server.MapPath("~/" + strFileName);

xmlDoc.Load(strFileLocation);

XmlNodeList xmlNode = xmlDoc.GetElementsByTagName("UserSettings");

How do i read directly based on element "Source" ( example for m y above xml : read by passing settings/subscriptions to match the "Source" element ?) I know its real basics, but really confused!

2
  • Could you please rephrase your question? Commented Dec 10, 2011 at 6:43
  • Hi AVD, Do you need any more info? Commented Dec 10, 2011 at 6:55

2 Answers 2

1

Use Linq-XML (import System.Xml.Linq namespace).

XDocument doc = XDocument.Load(filename);
string value = doc.Root.Element("Source").Value;
Sign up to request clarification or add additional context in comments.

Comments

0

I'd use an XmlDocument instead, in combination with SelectSingleNode that accepts an XPath expression. The following is untested:

XmlDocument doc = new XmlDocument();
strFileName = [...]
doc.Load(strFileName);
sourcetext=doc.SelectSingleNode("/UserSettings/Source").InnerText;

Edit:

Here's a rough example on how to get the DestinationController based on Source.

XmlDocument doc = new XmlDocument();
strFileName = [...]
doc.Load(strFileName);
dctext=doc.SelectSingleNode("/UserSettings/[Source=\"Your desired source\"]/DestinationController").InnerText;

6 Comments

thank you, what will the sourcetext contain? COz based on that I need to read the corresponding DstinationController and action , How do I do that?
sourcetext will contain the text of the <Source> tag and its children.
ok is it possible to get the node tag(Usersetting) based on element(source)?
Yes, you can do that either in your own code or directly in the xpath expression.
Added a new xpath expression.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.