• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Changing XML Node name using C#

HI ;
I have the following XML
 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<ActualKPIAxisMinuteImport>
  <ActualKPIAxisMinute>
     <XrefCode>00218</XrefCode>
     <DayId>2011-04-04</DayId>
     <MinuteId>2011-04-04T10:00:00</MinuteId>
     <AxisId>8</AxisId>
     <TicXrefCode>Sales</TicXrefCode>
     <KpiId>29</KpiId>
     <Value>4</Value>
     </ActualKPIAxisMinute>
  <ActualKPIAxisMinuteImport>
<ActualKPIAxisMinuteImport>


And I Want to change The first Node <ActualKPIAxisMinuteImport> to <ActualKPIAxisMinuteImport AggregateToDay="true"> to get the following result

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<ActualKPIAxisMinuteImport AggregateToDay="true">
  <ActualKPIAxisMinute>
     <XrefCode>00218</XrefCode>
     <DayId>2011-04-04</DayId>
     <MinuteId>2011-04-04T10:00:00</MinuteId>
     <AxisId>8</AxisId>
     <TicXrefCode>Sales</TicXrefCode>
     <KpiId>29</KpiId>
     <Value>4</Value>
     </ActualKPIAxisMinute>
  <ActualKPIAxisMinuteImport>
<ActualKPIAxisMinuteImport>

How to do that with C# ?
0
Fatehbourghoud
Asked:
Fatehbourghoud
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
What you are actually wanting to do is to append an attribute to the documents root node. You can do this with something like:
            string filename = "C:\\test.xml";

            XmlDocument doc = new XmlDocument();
            doc.Load(filename);

            XmlAttribute attr = doc.CreateAttribute("AggregateToDay");
            attr.Value = "true";

            doc.DocumentElement.Attributes.SetNamedItem(attr);
            doc.Save(filename);

Open in new window

0
 
FatehbourghoudAuthor Commented:
Thanks Carl ;

I really appriciate your help
0
 
FatehbourghoudAuthor Commented:
Very helpfull and excelent solution

Thanks Carl
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now