Solved

Xml template

Posted on 2006-10-31
9
309 Views
Last Modified: 2013-11-18
<?xml version="1.0" encoding="utf-8" ?>
<Ack>
<Msg>
      <ori>originator ata</ori>
      <rec>recipient data</rec>
      <Type></Type>
</Msg>
<MsgId></MsgId>
<Sign>
<data></data>
<keyId></keyId>
<algorithm></algorithm>
</Sign>
</Ack>

This is the xml template in my c# project. I have to load this xml file at runtime and insert the calculated value in the specified tag (Type) and save it for future modification. How can i add the tag value in the xml template?
0
Comment
Question by:Rahamathulla_J
  • 6
  • 3
9 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17842515

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"
<?xml version='1.0' encoding='utf-8' ?>
<Ack>
<Msg>
     <ori>originator ata</ori>
     <rec>recipient data</rec>
     <Type></Type>
</Msg>
<MsgId></MsgId>
<Sign>
<data></data>
<keyId></keyId>
<algorithm></algorithm>
</Sign>
</Ack>";

XmlNode typeElement = doc.DocumentElement.SelectSingleNode("Msg/Type");
typeElement.InnerText = [value];
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17842547
Sorry, this is a better version that actually compiles :)
Just replace "value" on the last line with your calculated value.

XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"
                  <Ack>
                  <Msg>
                       <ori>originator ata</ori>
                       <rec>recipient data</rec>
                       <Type></Type>
                  </Msg>
                  <MsgId></MsgId>
                  <Sign>
                  <data></data>
                  <keyId></keyId>
                  <algorithm></algorithm>
                  </Sign>
                  </Ack>");

            XmlNode typeElement = doc.DocumentElement.SelectSingleNode("Msg/Type");
            typeElement.InnerText = "value";
0
 

Author Comment

by:Rahamathulla_J
ID: 17848117
Hi,

Actually what my problem is i am already having one xml file in the bin folder of my project so i have to load that xml file. If i use the above code i am not able to use .Loadxml it throws system exception.

So i have use Load method.

Even though i give typeElement.InnerText = "0" it is not reflected in the xml file.

can u identify what is the problem?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17848142
Just switch from using the doc.LoadXml("<xml/>") method to using the doc.Load("path to your xml file") Method.

If you get an exception when you load your xml it is either because it cannot read the file or because your xml is badly formed.

Once you have set the value of the element you need to dave the document back to the file using doc.Save("path to your xml file");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Rahamathulla_J
ID: 17848329
The xml file is in my application's bin folder. Moreover doc.save() methods expects the parameter

System.XML.XMLWriter then how can i pass the file path.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 17848535
>>The xml file is in my application's bin folder.
So pass the path to the Load() method then. What is the problem ?

>>...doc.save() methods expects the parameter
>>System.XML.XMLWriter then how can i pass the file path.

XmlDocument.Save() has several overloads which take a filepath as a string, a TextWriter, a Stream and an XmlWriter.
If you want to use an XmlReader then create an XmlTextReader and pass that your path to your xml file :

    doc.Load(new XmlTextReader(@"c:\documents and settings\username\my documents\visual studio projects\my project\bin\my xml file.xml"));

That is what the Load method does anyway if you pass it a string.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17902735
Hi, has this answered your question ?
0
 

Author Comment

by:Rahamathulla_J
ID: 17912810
yes thanks
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17912919
Another B Grade, please justify ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now