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

0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is XElement is not sufficient? Do we really need XDocument? because there is no root, declaration in the xml.
Thank you for the solution and its working perfectly, but what if i want to add xmlinnerText in multiple place in MainxmlText cuz in this case it will add it only before the last tag. also XDocument will effect the memory?
0

I all you are trying to achieve is insert a piece of xml to an xml string you could use the XElement class to achieve that.

For ex.

var xmlString = "<person></person>";

var xEl = XElement.Parse(xmlString);

xEl.Add(XElement.Parse("<name>vijay</name>"));

Console.Writeline(XEl);

O/P

<person><name>vijay</name></person>

Google for XElement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.