Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:23:35+00:00 2026-06-11T22:23:35+00:00

i am trying to read an xml string and then modify it by inserting

  • 0

i am trying to read an xml string and then modify it by inserting another xml string, i dont know how to do this, i was struggling since i am a beginner in this area. i search the net and i was really confussing between xmlWriter and xmlReader cuz i dont want to use xmlDocument
i found this from microsoft
http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx

so my code is like this

 String Mainxmltext = "<ClosedKPICalls>" +
    "<ServiceInfo>  <ServiceNo>HR011</ServiceNo> <ServiceName>Petty cash</ServiceName>    "+
    "<RecCount>0</RecCount>  <RegdKPI>2</RegdKPI>  </ServiceInfo> </ClosedKPICalls>";

    String xmlinnerText = "<TaskDetails> <name>Jhone</name> <Description>just for testing</Description> </TaskDetails>";

    StringBuilder output = new StringBuilder();
    using (XmlReader reader = XmlReader.Create(new StringReader(Mainxmltext)))
    {
        XmlWriterSettings ws = new XmlWriterSettings();
        ws.Indent = true;
        using (XmlWriter writer = XmlWriter.Create(output, ws))
        {

            // Parse the file and display each of the nodes.
            while (reader.Read())
            {
                switch (reader.NodeType)
                {

                    case XmlNodeType.Element:
                        writer.WriteStartElement(reader.Name);                           
                        break;
                    case XmlNodeType.Text:
                        writer.WriteString(reader.Value);

                        break;
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        writer.WriteProcessingInstruction(reader.Name, reader.Value);
                        break;
                    case XmlNodeType.Comment:
                        writer.WriteComment(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        writer.WriteFullEndElement();                           
                        break;
                }
            }
        }
    }


    MessageBox.Show(output.ToString());

}

so i am successfully read the first string now how can i insert the second test before the closing tag of the main xml i mean before and my xml should look like this

<ClosedKPICalls>
<ServiceInfo>  
<ServiceNo>HR011</ServiceNo> 
<ServiceName>Petty cash</ServiceName>  
<RecCount>0</RecCount>  
<RegdKPI>2</RegdKPI>  
</ServiceInfo>
<TaskDetails> 
<name>Jhone</name> 
<Description>just for testing</Description> 
</TaskDetails>
</ClosedKPICalls>

please help
thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:23:37+00:00Added an answer on June 11, 2026 at 10:23 pm

    Linq2Xml is easier to use.

    String Mainxmltext = "<ClosedKPICalls>" +
            "<ServiceInfo>  <ServiceNo>HR011</ServiceNo> <ServiceName>Petty cash</ServiceName>    " +
            "<RecCount>0</RecCount>  <RegdKPI>2</RegdKPI>  </ServiceInfo> </ClosedKPICalls>";
    
    String xmlinnerText = "<TaskDetails> <name>Jhone</name> <Description>just for testing</Description> </TaskDetails>";
    
    XDocument xDoc = XDocument.Parse(Mainxmltext);
    xDoc.Root.Add(XElement.Parse(xmlinnerText));
    var newxml = xDoc.ToString();
    

    That is all to get your desired xml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read the following xml stream but am really struggling. <channelSnapshot
Hi I am trying to read xml string and trying to replace from old
im trying to serialize some object as xml and then read it back the
I'm trying to parse some XML (html) I downloaded using WebRequest.Create() and then read
looks like I need help again! :-/ Im trying to read this XML file
I have this inno script where i am trying to read a xml file
I have one .net windows application I'm trying to read .xml file from c#
I am trying to read some XML code from a website, and am having
I am trying to read the xml data in to php. I managed to
I am trying to read an XML file using objective C language and parse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.