C# 如何通过前缀属性查找XML元素

C# 如何通过前缀属性查找XML元素,c#,xml,C#,Xml,我有以下XML: <r xmlns:p="p" xmlns:s="s" p:p="1"> <d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"> <r uid="{B19D0460-69C9-4FEC-A882-41FFA77B582F}" p:before="r[@uid='{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}']" s:ds="/Polecane" s:id=

我有以下XML:

<r xmlns:p="p" xmlns:s="s" p:p="1">
    <d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
        <r uid="{B19D0460-69C9-4FEC-A882-41FFA77B582F}" p:before="r[@uid='{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}']" s:ds="/Polecane" s:id="{C3AC52FB-8F51-4B44-A3EE-55F4C2EAF83F}" s:par="" s:ph="moviesplaceholder" />

        <r uid="{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}" p:before="r[@uid='{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}']" s:ds="/HITY" s:id="{7A80D822-8D6A-40C4-92D4-2FA22C596339}" s:par="" s:ph="moviesplaceholder" />

        <r uid="{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}" p:before="r[@uid='{B6036949-B723-42CE-9271-64DF299450DC}']" s:ds="/VOD" s:id="{557800E7-953B-4830-8691-3FFB964712A8}" s:par="" s:ph="moviesplaceholder" />
    </d>
</r>

然后获取其“uid”并将其设置为HITY node中的“p:before”?

您可以使用如下内容:

  //variable which holds xml data
  string xml = "<r>......</r>";
  XmlDocument doc = new XmlDocument();
  //XML in XmlDocument
  doc.LoadXml(xml);
  XmlNodeList nodes = doc.SelectNodes("//d/r");
  string uid = "";
  //Parse all r nodes
  foreach (XmlNode node in nodes)
  {
    if (node.Attributes["s:ds"].InnerText == "/Polecane")
    {
      uid = node.Attributes["uid"].InnerText;
    }
  }
  foreach (XmlNode node in nodes)
  {
    if (node.Attributes["s:ds"].InnerText == "/HITY")
    {
      //Update attribute
      node.Attributes["p:before"].InnerText = uid;
    }
  }
  doc.Save(@"C:\tmp\xmldocument.xml");
//保存xml数据的变量
字符串xml=“……”;
XmlDocument doc=新的XmlDocument();
//XML文档中的XML
doc.LoadXml(xml);
XmlNodeList nodes=doc.SelectNodes(“//d/r”);
字符串uid=“”;
//解析所有r节点
foreach(节点中的XmlNode节点)
{
if(node.Attributes[“s:ds”].InnerText==“/Polecane”)
{
uid=node.Attributes[“uid”].InnerText;
}
}
foreach(节点中的XmlNode节点)
{
if(node.Attributes[“s:ds”].InnerText==“/HITY”)
{
//更新属性
node.Attributes[“p:在”]之前。InnerText=uid;
}
}
doc.Save(@“C:\tmp\xmldocument.xml”);
使用XDocument:

string xml = @"<r xmlns:p=""p"" xmlns:s=""s"" p:p=""1"">
    <d id=""{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"">
        <r uid=""{B19D0460-69C9-4FEC-A882-41FFA77B582F}"" p:before=""r[@uid='{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}']"" s:ds=""/Polecane"" s:id=""{C3AC52FB-8F51-4B44-A3EE-55F4C2EAF83F}"" s:par="""" s:ph=""moviesplaceholder"" />

        <r uid=""{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}"" p:before=""r[@uid='{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}']"" s:ds=""/HITY"" s:id=""{7A80D822-8D6A-40C4-92D4-2FA22C596339}"" s:par="""" s:ph=""moviesplaceholder"" />

        <r uid=""{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}"" p:before=""r[@uid='{B6036949-B723-42CE-9271-64DF299450DC}']"" s:ds=""/VOD"" s:id=""{557800E7-953B-4830-8691-3FFB964712A8}"" s:par="""" s:ph=""moviesplaceholder"" />
    </d>
</r>";

XNamespace nsS = "s";
XNamespace nsP = "p";

XDocument xd = XDocument.Parse(xml);

XElement elPolecane = xd.Descendants("r").Where(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value == "/Polecane").FirstOrDefault();
if (elPolecane != null)
{
    string uid = elPolecane.Attribute("uid").Value;
    XElement elHITY = xd.Descendants("r").Where(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value == "HITY").FirstOrDefault();
    if (elHITY != null)
    {
        elHITY.Attribute(nsP + "before").SetValue("r[@uid='" + uid + "']");
    }
}

Console.WriteLine(xd.ToString());

Console.ReadLine();
stringxml=@”
";
XNamespace nsS=“s”;
XNamespace nsP=“p”;
XDocument xd=XDocument.Parse(xml);
XElement elPolecane=xd.substands(“r”)。其中(e=>(e.Attribute(nsS+“ds”)!=null)和&e.Attribute(nsS+“ds”)。Value==“/Polecane”)。FirstOrDefault();
if(elPolecane!=null)
{
字符串uid=elPolecane.Attribute(“uid”).Value;
XElement elHITY=xd.substands(“r”)。其中(e=>(e.Attribute(nsS+“ds”)!=null)和&e.Attribute(nsS+“ds”)。Value==“HITY”).FirstOrDefault();
if(elHITY!=null)
{
elHITY.Attribute(nsP+“before”).SetValue(“r[@uid=”“+uid+””]);
}
}
Console.WriteLine(xd.ToString());
Console.ReadLine();

您应该向我们展示您的尝试。这将减少您的否决票。您看到了
node.Attributes[“s:ds”]。InnerText
返回正确的值吗?我会非常惊讶,因为这对元素不起作用,而且名称空间前缀并不重要。是的,如果我循环使用,这是正确的,但是XPath不适用于这个“s:ds”。这就是为什么我不使用SelectSingleNode。好吧,它可能(只是可能)不会崩溃,但它不是“正确的”。只要实际名称空间保持不变,就允许更改名称空间前缀。前缀一更改,代码就会中断。
string xml = @"<r xmlns:p=""p"" xmlns:s=""s"" p:p=""1"">
    <d id=""{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"">
        <r uid=""{B19D0460-69C9-4FEC-A882-41FFA77B582F}"" p:before=""r[@uid='{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}']"" s:ds=""/Polecane"" s:id=""{C3AC52FB-8F51-4B44-A3EE-55F4C2EAF83F}"" s:par="""" s:ph=""moviesplaceholder"" />

        <r uid=""{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}"" p:before=""r[@uid='{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}']"" s:ds=""/HITY"" s:id=""{7A80D822-8D6A-40C4-92D4-2FA22C596339}"" s:par="""" s:ph=""moviesplaceholder"" />

        <r uid=""{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}"" p:before=""r[@uid='{B6036949-B723-42CE-9271-64DF299450DC}']"" s:ds=""/VOD"" s:id=""{557800E7-953B-4830-8691-3FFB964712A8}"" s:par="""" s:ph=""moviesplaceholder"" />
    </d>
</r>";

XNamespace nsS = "s";
XNamespace nsP = "p";

XDocument xd = XDocument.Parse(xml);

XElement elPolecane = xd.Descendants("r").Where(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value == "/Polecane").FirstOrDefault();
if (elPolecane != null)
{
    string uid = elPolecane.Attribute("uid").Value;
    XElement elHITY = xd.Descendants("r").Where(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value == "HITY").FirstOrDefault();
    if (elHITY != null)
    {
        elHITY.Attribute(nsP + "before").SetValue("r[@uid='" + uid + "']");
    }
}

Console.WriteLine(xd.ToString());

Console.ReadLine();