开发者

Adding Elements,Nodes and Text in XMl in .Net

I want to add an elements ,nodes to an existing Xml. I am using the below mentioned Code ,but not getting proper output of Xml.

 XmlNode root=doc.SelectSingleNode("DB");
 XmlElement elem = doc.CreateElement("ScopeCleanUp");
 root.AppendChild(elem);
 XmlNode rootLoc= doc.SelectSingleNode("DBSync/ScopeCleanUp");
 X开发者_C百科mlElement elemLoc = doc.CreateElement("LocalScope");           
 XmlNode rootScope = doc.SelectSingleNode("DBSync/ScopeCleanUp/LocalScope");
 for (int s = 0; s < 2; s++)
 {
     XmlElement elemScope = doc.CreateElement("ScopeName");
     elemScope.InnerText = s.ToString();
     rootLoc.AppendChild(elemScope);                       
 }
 rootLoc.AppendChild(elemLoc);

I need the Xml as below Output.

<DB>
<ScopeCleanUp>
<LocalDataBase>
<ScopeName>1</ScopeName> 
<ScopeName>2</ScopeName> 
  </LocalDataBase>
</ScopeCleanUp>
</DB>


Something like this should do the trick:

        XmlElement db = doc.CreateElement("DB");
        XmlElement scope = doc.CreateElement("ScopeCleanUp");
        XmlElement local = doc.CreateElement("LocalDataBase");

        doc.AppendChild(db);
        db.AppendChild(scope);
        scope.AppendChild(local);

        for (int i = 0; i < 2; i++)
        {
            XmlElement elem = doc.CreateElement("ScopeName");
            elem.InnerText = i.ToString();
            local.AppendChild(elem);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜