如何在xml C#中删除root标签

时间:2017-04-03 10:59:49

标签: c# xml

以下XML是我的输入XML

<?xml version="1.0" encoding="utf-8"?>
      <units xmlns="http://www.elsevier.com/xml/ani/ani" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ce="http://www.elsevier.com/xml/ani/common" xsi:schemaLocation="http://www.elsevier.com/xml/ani/ani http://www.elsevier.com/xml/ani/ani512-input-CAR.xsd">
<unit type="journal" xmlns="">
<unit-info>
  <timestamp>2017-01-08T02:03:14Z</timestamp>
  <order-id>12535892</order-id>
  <parcel-id>none</parcel-id>
  <unit-id>4756202</unit-id>
</unit-info>
<unit-content>
  <bibrecord>
    <item-info>
      <status state="new" stage="S300" />
    </item-info>
    <head>
      <citation-info>
        <citation-type code="jo" />
        <citation-language xmllang="ENG" />
        <abstract-language xmllang="ENG" />
        <author-keywords>
          <author-keyword>Stroke</author-keyword>
          <author-keyword> cerebral ischaemia</author-keyword>
          <author-keyword> Neuro-protection</author-keyword>
          <author-keyword> Neuro-protective agents</author-keyword>
        </author-keywords>
      </citation-info>
      <citation-title xmllang="ENG" original="y">
        <titletext>PATHOGENESIS AND NEURO-PROTECTIVE AGENTS OF STROKE</titletext>
      </citation-title>
      <abstracts>
        <abstract>
          <cepara>ABSTRACT: Stroke remains worlds second leading cause of mortality; and globally most frequent cause of long-lasting disabilities. The ischaemic pathophysiologic cascade leading to neuronal damage consists of peri-infarct depolarization, excitotoxicity, inflammation, oxidative stress, and apoptosis. Despite plethora of experimental evidences and advancement into the development of treatments, clinical treatment of acute stroke still remains challenging. Neuro-protective agents, as novel therapeutic strategy confer neuro-protection by targeting the pathophysiologic mechanism of stroke. The aim of this review is discussion of summary of the literature on stroke pathophysiology, current preclinical research findings of neuroprotective agents in stroke and possible factors that were responsible for the failure of these agents to translate in human stroke therapies.</cepara>
        </abstract>
      </abstracts>
      <correspondence>
        <person>
          <ceinitials>M.</ceinitials>
          <cesurname>Mubarak</cesurname>
        </person>
        <affiliation>
          <organization> Bayero University Kano</organization>
          <organization> Nigeria. Email: mubarakmahmad@yahoo.com</organization>
        </affiliation>
      </correspondence>
      <root>
        <author Seq="0">
          <Inital>A.El</Inital>
          <Surname>Khattabi</Surname>
          <Givenname>Abdelkrim</Givenname>
        </author>
      </root>
    </head>
  </bibrecord>
</unit-content>

从这个xml

我需要单独删除root标签,但我需要所有子元素

        <author Seq="0">
          <Inital>A.El</Inital>
          <Surname>Khattabi</Surname>
          <Givenname>Abdelkrim</Givenname>
        </author>

如何单独删除root标签。为此,我遵循以下代码

  XDocument CarXML = new XDocument();
        CarXML.Add(Root);
        CarXML.Descendants("root").Remove();
        CarXML.Save(@"CAR.XML");

但是这段代码删除了所有xml标签。如何单独删除根元素。我需要子元素

1 个答案:

答案 0 :(得分:0)

CarXML.ReplaceNodes(CarXML.Descendants("author").FirstOrDefault());

这将使用名为author的第一个后代替换XML的全部内容。