-3

我有下面的xml

<databaseserver>
   <server name="server1" serverid="1">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>
   <server name="server2" serverid="2">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>   
</databaseserver>

如何追加新节点

<server name="" serverid="3">
    <databases>
            <database dbid="" dbname="" />        
    </databases>
   </server>

到现有的 xml,使最终输出看起来像

<databaseserver>
   <server name="server1" serverid="1">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>
   <server name="server2" serverid="2">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server> 
   <server name="" serverid="3">
    <databases>
            <database dbid="" dbname="" />        
    </databases>
   </server>  
</databaseserver>
4

1 回答 1

2

xDoc和你之前的问题一样

xDoc.Root.Add(new XElement("server", 
                new XAttribute("name","server4"),
                new XAttribute("serverid","4"),
                new XElement("databases", 
                    new XElement("database", 
                        new XAttribute("dbid","db4"), 
                        new XAttribute("dbname","name4")))));
于 2013-06-09T13:47:51.280 回答