Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否读取基于根节点和其中一个元素的其他xml元素?_Xml_Asp.net Mvc 3_Xmlnode - Fatal编程技术网

是否读取基于根节点和其中一个元素的其他xml元素?

是否读取基于根节点和其中一个元素的其他xml元素?,xml,asp.net-mvc-3,xmlnode,Xml,Asp.net Mvc 3,Xmlnode,下面给出了我的XML <UrlRoutes> <ActivityPR> <Source>activity/editactivity</Source> <DestinationController>Activity</DestinationController> <DestinationAction>Editactivity</DestinationAction>

下面给出了我的XML

<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>

我正在尝试使用下面的代码获取父节点,如果有问题,请告诉我

XmlElement xmlNode = xmlDoc.GetElementById(SourceX);
XmlNode parent = xmlNode.ParentNode;
现在,对于父节点的组合,如ActivityPR或Usersettings和Source,我必须找到相应的DestinationController和DestinationAction

我该怎么做?相对于LINQ,您更喜欢传统的XML,因为其他代码都是这种形式的

if(node!=null)
        {
            XmlElement routeElement = (XmlElement)node;


            strController = routeElement.GetElementsByTagName("DestinationController")[0].InnerText.ToString();
            strAction = routeElement.GetElementsByTagName("DestinationAction")[0].InnerText.ToString();


        }

我假设您已经将XML加载到XmlDocument中-正确吗

在这种情况下,您应该能够使用如下内容:

string xpath = string.Format("/UrlRoutes/*[Source='{0}']", sourceX);
XmlNode node = xmlDoc.SelectSingleNode(xpath);

if(node != null)
{
    // use the node to do whatever you need to do
}
xpath表达式基本上选择/UrlRoutes下的任何节点,该节点包含一个值为给定字符串的元素。这意味着:的值必须是唯一的

更新:如果您知道只想在节点内部搜索,可以使XPath表达式更具选择性:

string parentNode = "UserSettings";
string sourceX = "settings/subscriptions";

string xpath = string.Format("/UrlRoutes/{0}[Source='{1}']", parentNode, sourceX);
但是使用这种更具选择性的XPath,当示例中有sourceX=activity/editactivity时,您将无法找到节点,因为它不在节点内部

更新2:我可能会使用以下代码获取节点内的元素:

if(node != null)
{
   string dc = node.SelectSingleNode("DestinationController").InnerXml;
   string da = node.SelectSingleNode("DestinationAction").InnerXml;
}

这样,您就不需要首先转换为XmlElement

谢谢Marc,xml似乎是这样的,比如说xml中可能有多个UserSettings节点,但是UserSettings和source的组合是唯一的,也就是说,我想我需要搜索这两个itemsparent节点和SOurceX的组合,我不知道你的代码在那里是否有效,会吗?@AdarshK:只要源代码的值在整个XML文档中是唯一的,那么我的代码就可以正常工作。哦,rite,源代码似乎是唯一的,因此我相信您的原始代码可以正常工作,对吗?@AdarshK:是的,如果是唯一的,那么您可以使用我的第一个XPath表达式。我在问题的末尾添加了一段代码,你能确认它是否有效吗?
string parentNode = "UserSettings";
string sourceX = "settings/subscriptions";

string xpath = string.Format("/UrlRoutes/{0}[Source='{1}']", parentNode, sourceX);
if(node != null)
{
   string dc = node.SelectSingleNode("DestinationController").InnerXml;
   string da = node.SelectSingleNode("DestinationAction").InnerXml;
}