繁体   English   中英

Xaml中的XPath选择节点

[英]XPath select node in Xaml

我想匹配xaml文件中的一个节点,我的文件如下所示:

一些Xaml(类似于xml)的输入:

<SomeNode> <!-- this is root btw -->
    <!-- [...] -->
    <SomeNode.AnyProperty>
        <!-- [...] -->
    </SomeNode.AnyProperty>
    <!-- [...] -->
</SomeNode>

我只想对“ SomeNode.AnyProperty ”部分进行数学运算。 之后,我想用一个生成的节点替换找到的节点。

关于工作的XPath表达式有什么建议吗? 我尝试了一些常见的表达式,例如那些将应用于普通xml文件的表达式:“ SomeNode.AnyProperty”。 但是可以肯定的是,这没有用。

工作解决方案:

感谢所有支持。 问题不在于XPath表达式本身。 此外,它是我的xaml输入文件的名称空间声明。 为了避免此命名空间问题,我使用了@malkam解决方案的修改版本。

  XDocument doc = XDocument.Load("someFile.xaml");
  //Get required element
  XElement nodeToReplace = doc.Elements().Where(x => x.Name.LocalName == "SomeNode.AnyProperty").FirstOrDefault() as XElement;
  //replace it with requried element
  nodeToReplace.ReplaceWith(someOtherNodeIGeneratedEarlier);
  doc.Save("someFile_editet.xaml");

尝试这个。

//Load xml in XElement
string xml="xml";
XElement xmlTree=XElement.Parse(xml);
//Get required element
XElement child = xmlTree.Element("SomeNode.AnyProperty");
//replace it with requried element
child.ReplaceWith(
    new XElement("NewChild", "new content")
);

有关更多详细信息,请参见下面的链接。

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement_methods%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-IN/library/bb302711.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM