简体   繁体   中英

How to copy a node xml and appended to the specified node using linq to XML

I have an xml,like this :

<advReqData>
  <sessionid></sessionid>
  <akc190></akc190>
  <meds>
    <med>
      <idx></idx>
      <alc400></alc400>
    </med>
  </meds>
</advReqData>

I would like to using linq to xml make XML into the following structure:

<advReqData>
  <sessionid></sessionid>
  <akc190></akc190>
  <meds>
    <med>
      <idx></idx>
      <alc400></alc400>
    </med>
    <med>
      <idx></idx>
      <alc400></alc400>
    </med>
  </meds>
</advReqData>

That is, in the meds node under the addition of a new med node, the new med node and the original document med node structure is the same.

How do I do it?

This will get the first element in "meds" and then add it to the end

var fn = xelement.Element("meds").FirstNode;
xelement.Element("meds").Add(fn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM