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

Noora
  • 319
  • 3
  • 10
  • 26

2 Answers2

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

L.B
  • 114,136
  • 19
  • 178
  • 224
  • Is XElement is not sufficient? Do we really need XDocument? because there is no root, declaration in the xml. – VJAI Sep 26 '12 at 14:17
  • 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? – Noora Sep 27 '12 at 07:13
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.

VJAI
  • 32,167
  • 23
  • 102
  • 164