Solved

Xml template

Posted on 2006-10-31
9
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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!
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

734 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