读取,循环和编辑XML

时间:2014-06-16 14:38:07

标签: xml vb.net

我正在尝试读取xml文件并将两个节点创建为子节点。以下是xmlfile的示例内容:

<?xml version="1.0" encoding="utf-8"?>
<ClaimBatch>
    <Claim>
      <chargeableCodeGroup>Consultation</chargeableCodeGroup>
      <schemeSurcharge>0</schemeSurcharge>
      <levy>00.00</levy>
      <code>M71.38</code>
      <qualifier>P</qualifier>
    </Claim>
  </Claim>
</ClaimBatch>

I want to read this file and take <Code> and <qualifier> add them to <diagnosis>. Below is an example of the desired output

<?xml version="1.0" encoding="utf-8"?>
<ClaimBatch>
    <Claim>
      <chargeableCodeGroup>Consultation</chargeableCodeGroup>
      <schemeSurcharge>0</schemeSurcharge>
      <levy>00.00</levy>
      <Diagnosis>
        <code>M71.38</code>
        <qualifier>P</qualifier>
      </Diagnosis>
    </Claim>
</ClaimBatch>

我已尝试过以下代码,但无效

Dim cNode As XmlNode = xmlDoc.SelectSingleNode("/ClaimBatch/Claims/Claim")
'Dim currNode As XmlNode
' Select the 3rd child node of <C>
Dim thirdChildNode As XmlNode = cNode.ChildNodes(24)
Dim docFrag As XmlDocumentFragment = xmlDoc.CreateDocumentFragment()
docFrag.InnerXml = "<Diagnosis>" + "</Diagnosis>"

' insert the availability node into the documentcurrNode = doc.DocumentElement.FirstChild;
cNode.InsertAfter(docFrag, thirdChildNode

1 个答案:

答案 0 :(得分:0)

使用LINQ to XML可以使用以下内容。

For Each claim In xmlDoc...<Claim>
    claim.Add(
        <Diagnosis>
            <%= claim.<code> %>
            <%= claim.<qualifier> %>
        </Diagnosis>
    )
    claim.<code>.Remove()
    claim.<qualifier>.Remove()
Next