Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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# 如何搜索非结构化XML?_C#_Xml - Fatal编程技术网

C# 如何搜索非结构化XML?

C# 如何搜索非结构化XML?,c#,xml,C#,Xml,我有这样的想法: <group name="name1" ... > <group name="name2" ... > <image name="test1" ...> <image name="test2" ...></image> <group name="test98"...> <image name="test67" ...> &l

我有这样的想法:

<group name="name1" ... >
  <group name="name2" ... >
    <image name="test1" ...>
      <image name="test2" ...></image>
      <group name="test98"...>
        <image name="test67" ...>
          <group name="test987"...>
            <text name="asddd"...></text>
          </group>
        </image>
      </group>
      <group name="name22" ... >
        <image name="test3" ...></image>
      </group>
    </image>
    <image name="test4" ...>
      <text name="asddd"...></text>
    </image>
  </group>
</group>

正如你所看到的,这是没有组织的。也不是固定的,既不是节点名也不是顺序。 我不知道我要改变什么节点。(除了团体和形象,可能还有更多)

我想要的是克隆第一个节点,然后搜索特定属性以更改其值。其中一些具有名为“路径”的属性,而另一些仅具有名为“左”的属性


您认为将xml转换为文本更容易吗?

将xml加载到
XmlDocument
(或
XDocument
)将使您能够使用XPath查询,并且您可以很容易地找到属性名称,如下例所示

public class StackOverflow_7276178
{
    const string XML = @"<group name='name1'  >
  <group name='name2'  >
    <image name='test1' >
      <image name='test2' ></image>
      <group name='test98'>
        <image name='test67' >
          <group name='test987'>
            <text name='asddd' path='myPath'></text>
          </group>
        </image>
      </group>
      <group name='name22'  >
        <image name='test3' left='myLeft'></image>
      </group>
    </image>
    <image name='test4'>
      <text name='asddd'></text>
    </image>
  </group>
</group>";

    public static void Test()
    {
        XmlDocument doc = new XmlDocument();
        doc.PreserveWhitespace = true;
        doc.LoadXml(XML);

        foreach (XmlAttribute pathAttr in doc.SelectNodes("//@path"))
        {
            pathAttr.Value = pathAttr.Value + "_modified";
        }

        foreach (XmlAttribute leftAttr in doc.SelectNodes("//@left"))
        {
            leftAttr.Value = leftAttr.Value + "_modified";
        }

        Console.WriteLine(doc.OuterXml);
    }
}
公共类堆栈溢出\u 7276178
{
常量字符串XML=@“
";
公共静态无效测试()
{
XmlDocument doc=新的XmlDocument();
doc.PreserveWhitespace=true;
doc.LoadXml(XML);
foreach(doc.SelectNodes(“/@path”)中的xmldattribute pathAttr)
{
路径属性值=路径属性值+“_modified”;
}
foreach(doc.SelectNodes(“/@left”)中的XmlAttribute leftAttr)
{
leftAttr.Value=leftAttr.Value+“_modified”;
}
Console.WriteLine(doc.OuterXml);
}
}

谢谢!我没有注意到我可以像这样使用xpath来过滤节点!很抱歉为这么简单的事费心。。如果不是太多,你能告诉我是否有可能知道属性的节点吗?我是说。。在“foreach”里面。再一次非常感谢。查看
OwnerElement
属性。