views:

312

answers:

4

I already implemented to create the XML file below with XmlTextWriter when application initialization.

And know I don't know how to update the childNode id value with XmlDocument & XmlNode.

Is there some property to update the id value? I tried InnerText but failed. thank you.

<?xml version="1.0" encoding="UTF-8"?>
<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  <License licenseId="" licensePath=""/>
  <DataCollections>
    <GroupAIDs>
      <AID id="100">
        <Variable id="200"/>
        <Variable id="201"/>
      </AID>
      <AID id="">
        <Variable id="205"/>
      </AID>
      <AID id="102"/>
    </GroupAIDs>
    <GroupBIDs>
      <BID id="2000">
        <AID id="100"/>
      </BID>
      <BID id="2001">
        <AID id="101"/>
        <AID id="102"/>
      </BID>
    </GroupBIDs>
    <GroupCIDs>
      <BID id="8"/>
      <BID id="9"/>
      <BID id="10"/>
    </GroupCIDs>
  </DataCollections>
</Equipment>
+1  A: 

Reading and Writing XML using c# Why not try out and come here with a specific question? also show us what have you done so far.

Shoban
Acturally I also seeked a lot of MSDN library article and did some practice, But I still didn't find the way to modify the Attribute of the related element. Such as **ID(attribute) of AID(element)** and **ID(attribute) of Variable(element)**. I can't find *SetAttribute* for **ChildNode**. Thanks.
Nano HE
+1  A: 

You need to do something like this:

// instantiate XmlDocument and load XML from file
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\test.xml");

// get a list of nodes - in this case, I'm selecting all <AID> nodes under
// the <GroupAIDs> node - change to suit your needs
XmlNodeList aNodes = doc.SelectNodes("/Equipment/DataCollections/GroupAIDs/AID");

// loop through all AID nodes
foreach (XmlNode aNode in aNodes)
{
   // grab the "id" attribute
   XmlAttribute idAttribute = aNode.Attributes["id"];

   // check if that attribute even exists...
   if (idAttribute != null)
   {
      // if yes - read its current value
      string currentValue = idAttribute.Value;

      // here, you can now decide what to do - for demo purposes,
      // I just set the ID value to a fixed value if it was empty before
      if (string.IsNullOrEmpty(currentValue))
      {
         idAttribute.Value = "515";
      }
   }
}

// save the XmlDocument back to disk
doc.Save(@"D:\test2.xml");
marc_s
It works fine! Thank you so much.
Nano HE
A: 

here i m doind some changes in above code-- to read or modify value of xml

let's we have xml for read and modify --

Vijay Bhaskar Semwal
A: 

now method to do chnage or modify

XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(@"BackEndConfig.xml"); XmlNodeList rootNodeList = xmldocument.GetElementsByTagName("Config"); foreach (XmlNode nd in rootNodeList) { TextBox tb1 = new TextBox(); tb1 = (TextBox)div_control.FindControl("txt_" + nd.Attributes["key"].Value); nd.Attributes["value"].Value = tb1.Text; } XmlNodeList rootNodeList1 = xmldocument.GetElementsByTagName("add"); foreach (XmlNode nd in rootNodeList1) { TextBox tb = new TextBox();

            tb = (TextBox)div_control.FindControl("txt_" + nd.Attributes["key"].Value);
            nd.Attributes["value"].Value = tb.Text;
        }
        //xmldocument.PreserveWhitespace = true;
        //XmlTextWriter wrtr = new XmlTextWriter(@"BackEndConfig.xml", Encoding.UTF8);
        //xmldocument.WriteTo(wrtr);
        //wrtr.Close();

or commented line caqn replce by xmldocument.Save(@"BackEndConfig.xml");

Vijay Bhaskar Semwal