Solved

Xml template

Posted on 2006-10-31
9
310 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 107
Web Form VB.Net  import CSV 4 39
Printing 1 51
Call Controller Action Method from ASPX 2 15
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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 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