Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Xml template

Posted on 2006-10-31
9
Medium Priority
?
316 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 750 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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 the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

926 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