基于元素读取XML节点?
我有一个示例 xml
<UserSettings>
<Source>settings/subscriptions</Source>
<DestinationController>UserSettings</DestinationController>
<DestinationAction>GetUserPreferenceSettings</DestinationAction>
</UserSettings>
使用标签名称(UserSettings)读取 XML 的方式如下所示。
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");
我如何直接基于元素“Source”读取(例如我上面的xml:通过传递设置/订阅来匹配“Source”元素来读取?)我知道它的真正基础知识,但真的很困惑!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Linq-XML
(导入System.Xml.Linq命名空间)。Use
Linq-XML
(import System.Xml.Linq namespace).我会使用 XmlDocument 结合使用SelectSingleNode 接受 XPath 表达式。以下内容未经测试:
编辑:
这是一个关于如何根据 Source 获取 DestinationController 的粗略示例。
I'd use an XmlDocument instead, in combination with SelectSingleNode that accepts an XPath expression. The following is untested:
Edit:
Here's a rough example on how to get the DestinationController based on Source.