简体   繁体   中英

Update an XML File with child node using vb.net

Following is the "by default" structure of my xml file I'm starting with it: The number of <Group> is ten

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--Properties General Logistic Plan-->
<Groups>
<Group id="01" GroupLevel="01" Description="First Group, Category first">
  <subGroups>

  </subGroups>
</Group>
<Group id="01" GroupLevel="02" Description=" First Group, Category second ">
  <subGroups>

  </subGroups>
</Group>
………
</Groups>

What I want is to pass a new child named: subGroup In “01”Group
<subGroup id=”x” GroupLevel=”01.01” Description=”blablabla”/>
Or additionally in “02”Group
<subGroup id=”z” GroupLevel=”01.02” Description=”blablabla”/>
More over I have to create a new <Group> under the <subGroups> (depending from the GroupLevel ) and the final structure will be looks like that:

<Groups>
<Group id="01" GroupLevel="01" Description="First Group, Category first">
  <subGroups>
    <subGroupB id=”10” GroupLevel=”01.01” Description=”blablabla”/>
      <GroupA id="21" GroupLevel="01.10" Description=" blablabla ">
         <subGroupC id=”10” GroupLevel=”01.21” Description=”blablabla”/>
        <GroupB id="33" GroupLevel="01.10.21" Description=" blablabla ">
            <subGroupD id=”12” GroupLevel=”01.10.21” Description=”blablabla”/>
        </GroupB>
      </GroupA>
  </subGroups>
</Group>
<Group id="01" GroupLevel="02" Description=" First Group, Category second ">
  <subGroups>

  </subGroups>
</Group>
………
</Groups>

The subGroups… B or C or D may be many and of course they have to passed each time in each own position.
I have already used multiple solutions for this; but no one fulfills my needs
This issue exceeds my personal programming experience
Is there someone who knows what I have to do?
If you please give me a full solution using vb.net; something like public class .

It needs to be like that because the final user needs to add a new subGroup depending on the GroupLevel for each own customer use.

You can use XDocument if you are using net framework3.5, here're some suggestions and you can continue to code and feedback;)

1) Please fetch out the specific node by XDocument's Descadents or Elements function, maybe it will be combined with First() or FirstOrDefault(). However, please first use XDocument's Load to load the full xml file, notice its path.

2) Then you can use XElment's Add method to add a new XElement (See MSDN sample: http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.add(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 )

3) In the end, please use XDocument's Save method to override the xml file.

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