1

This is my 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>

How can I delete the <application> node without deleting the <test> node ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pascal
  • 12,265
  • 25
  • 103
  • 195
  • 1
    Waiting in suspense for what you want to delete... ;) – Bertie Feb 01 '12 at 09:11
  • Lol Am assuming its the "test " tag you don't want to delete. i think you have to put them as code - on a new line with a four space indent, otherwise they're stripped. – Bertie Feb 01 '12 at 09:24
  • (note that whenever you want to use a `<`, it **must** be code-formatted: use the `{}` icon, or encase in backticks, or indent by four characters) – AakashM Feb 01 '12 at 09:25
  • @JacobusR No a stupid student did: String.Append(" – Pascal Feb 01 '12 at 09:49

3 Answers3

1

OK, so probably not my best answer, but hoping either this fits your need, or gives you a a good starting point. Firstly, I'm assuming that you're using C#. So, the way I did this, was to use the node you want to remove and select its child nodes and use them to create a new XDocument. There could be a neater way using Linq to achieve this, but I'm damned if I can see it! Anyway, hope this helps :

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());

I used this SO post as my guide : How to delete node from XML file using C#

Happy coding,
Cheers,
Chris.

Community
  • 1
  • 1
Bertie
  • 733
  • 5
  • 16
  • ok you just featch the "inner" Node and put it into a new xml document. Well... ok at least it should work ;-) – Pascal Feb 01 '12 at 10:41
  • That's it basically - yeah! :) Not quite as smarty as removing the the outer node. Annoying me, cos I'm sure there's a better way!! But this should server your purposes hopefully! – Bertie Feb 01 '12 at 11:00
0

Using XSLT you could do this:

<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>
Jack
  • 16,506
  • 19
  • 100
  • 167
  • Nice. Although, the Linq-to-XML tag was included by the OP. I've included a rather more crude solution above. – Mel Padden Feb 01 '12 at 10:22
0

Well there's this;

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();
    }

Although I'm a little curious as to why you would want to do this...

Mel Padden
  • 983
  • 1
  • 9
  • 21