Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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节点而不删除内部xml_C#_Linq_Linq To Xml_Xmlnode - Fatal编程技术网

C# 删除外部xml节点而不删除内部xml

C# 删除外部xml节点而不删除内部xml,c#,linq,linq-to-xml,xmlnode,C#,Linq,Linq To Xml,Xmlnode,这是我的xml: <application name="Test Tables"> <test> <xs:schema id="test" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> </xs:schema> </test> </applicatio

这是我的xml:

<application name="Test Tables">
<test>
  <xs:schema id="test" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">   
  </xs:schema> 
</test>
</application>


如何删除
节点而不删除
节点?

使用XSLT可以做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="application">
    <xsl:apply-templates select="test"/>
  </xsl:template>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

使用XSLT,您可以执行以下操作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="application">
    <xsl:apply-templates select="test"/>
  </xsl:template>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

好吧,就是这样

static void Main(string[] args)
    {
        string doc = @"
                    <application name=""Test Tables"">
                    <test>
                      <xs:schema id=""test"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema""                         xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">   
                      </xs:schema> 
                    </test>
                    </application>
                    ";
        XDocument xDoc = XDocument.Parse(doc);
        Console.Write(xDoc.ToString());
        Console.ReadLine();

        string descendants = xDoc.Descendants("application").DescendantNodes().First().ToString();
        xDoc = XDocument.Parse(descendants);
        Console.Write(xDoc.ToString());
        Console.ReadLine();
    }
static void Main(字符串[]args)
{
字符串doc=@“
";
XDocument xDoc=XDocument.Parse(doc);
Write(xDoc.ToString());
Console.ReadLine();
字符串子体=xDoc.subjects(“应用程序”).subjectantNodes().First().ToString();
xDoc=XDocument.Parse(后代);
Write(xDoc.ToString());
Console.ReadLine();
}
虽然我有点好奇你为什么要这么做…

嗯,有一个

static void Main(string[] args)
    {
        string doc = @"
                    <application name=""Test Tables"">
                    <test>
                      <xs:schema id=""test"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema""                         xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">   
                      </xs:schema> 
                    </test>
                    </application>
                    ";
        XDocument xDoc = XDocument.Parse(doc);
        Console.Write(xDoc.ToString());
        Console.ReadLine();

        string descendants = xDoc.Descendants("application").DescendantNodes().First().ToString();
        xDoc = XDocument.Parse(descendants);
        Console.Write(xDoc.ToString());
        Console.ReadLine();
    }
static void Main(字符串[]args)
{
字符串doc=@“
";
XDocument xDoc=XDocument.Parse(doc);
Write(xDoc.ToString());
Console.ReadLine();
字符串子体=xDoc.subjects(“应用程序”).subjectantNodes().First().ToString();
xDoc=XDocument.Parse(后代);
Write(xDoc.ToString());
Console.ReadLine();
}

虽然我有点好奇你为什么要这么做…

好的,所以可能不是我最好的答案,但希望这能满足你的需要,或者给你一个好的起点。首先,我假设您使用的是C#。因此,我这样做的方法是使用您要删除的节点,选择其子节点,并使用它们创建新的XDocument。使用Linq可以有一种更简洁的方法来实现这一点,但如果我能看到它,我就该死了!无论如何,希望这有助于:

var doc = XDocument.Load(@".\Test1.xml");

var q = (from node in doc.Descendants("application")
        let attr = node.Attribute("name")
        where attr != null && attr.Value == "Test Tables"
        select node.DescendantNodes()).Single();

var doc2 =  XDocument.Parse(q.First().ToString());
我用这篇文章作为我的指南:

快乐编码,
干杯,

克里斯。

好吧,也许不是我最好的答案,但希望这能满足你的需要,或者给你一个好的起点。首先,我假设您使用的是C#。因此,我这样做的方法是使用您要删除的节点,选择其子节点,并使用它们创建新的XDocument。使用Linq可以有一种更简洁的方法来实现这一点,但如果我能看到它,我就该死了!无论如何,希望这有助于:

var doc = XDocument.Load(@".\Test1.xml");

var q = (from node in doc.Descendants("application")
        let attr = node.Attribute("name")
        where attr != null && attr.Value == "Test Tables"
        select node.DescendantNodes()).Single();

var doc2 =  XDocument.Parse(q.First().ToString());
我用这篇文章作为我的指南:

快乐编码,
干杯,

克里斯。

等待你想要删除的内容……;)Lol我假设它是你不想删除的“测试”标签。我认为你必须把它们作为代码放在一个新的行上,有四个空格的缩进,否则它们会被剥离。(注意,每当你想使用
@JacobusR No时,一个愚蠢的学生会做:String.Append(“等待你想要删除的东西…”)Lol我假设它是“测试”“您不想删除的标记。我认为您必须将它们作为代码放在一个新行上,并带有四个空格的缩进,否则它们会被剥离。(请注意,每当您想使用
@JacobusR时,都会有一个愚蠢的学生这样做:String.Append(“不错。虽然,Linq to XML标记包含在OP中。我在上面包含了一个更粗糙的解决方案。不错。虽然,Linq to XML标记包含在OP中。我在上面包含了一个更粗糙的解决方案。好的,您只需对“内部”节点进行修饰,并将其放入一个新的XML文档中。嗯……好的,至少它应该可以工作;-)基本上就是这样-是的!:)没有移除外部节点那么聪明。让我恼火,因为我相信有更好的方法!!但这应该符合你的目的!好吧,你只需要将“内部”节点添加到一个新的xml文档中。好吧……好吧,至少它应该可以工作;-)基本上就是这样-是的!:)不像移除外部节点那样聪明。讨厌我,因为我相信有更好的方法!!但希望这能达到你的目的!