Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
C# Xaml中的XPath选择节点_C#_Xml_Xaml_Xpath - Fatal编程技术网

C# Xaml中的XPath选择节点

C# Xaml中的XPath选择节点,c#,xml,xaml,xpath,C#,Xml,Xaml,Xpath,我想匹配xaml文件中的一个节点,我的文件如下所示: 一些Xaml(类似xml)输入: 试试这个 //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.

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

一些Xaml(类似xml)输入: 试试这个

//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")
);
有关更多详细信息,请参阅以下链接


这个问题特别询问XPath。使用
XElement
istead of
XmlDocument
然后
XmlDocument的方法。SelectNode(XPath XPath)
实际解决了我的问题。我不得不使用变通方法来避免名称空间问题,但它最终完成了它的工作!我将为上面的问题添加解决方案。它对我有效。您究竟是如何使用该XPath的?我尝试在
XmlDocument.SelectNode(XPath XPath)
中使用它。我认为问题不在于XPath本身,而且我的xaml的名称空间声明是个邪恶的家伙。
  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")
);