Solved

Xml template

Posted on 2006-10-31
9
306 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now