locked
Reading XML, write to another XML doc RRS feed

  • Question

  • ive got an xml doc that uses the following structure
    <chapter>1</chapter><pair><term>some word</term><definition>definition of some word</definition></pair>

    what i need to do is read the doc, and every pair that has <chapter>1, write it to ch01.xml
    <chapter>2 needs to go to ch02.xml etc. any suggestions? im new to c#, so the more help, the better.

    Thanks in advance
    Monday, October 31, 2011 1:12 AM

Answers

  • > what i need to do is read the doc, and every pair that has <chapter>1, write it to ch01.xml

    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    ...
    var xe = XElement.Load(@"c:\temp\concentration.xml");
    foreach(var c in xe.Descendants("chapter"))
    {
        var path = @"c:\temp\chapter" + c.Value.PadLeft(2, '0') + ".xml";
        using(var xw = new XmlTextWriter(path, Encoding.Default))
            c.NextNode.WriteTo(xw);
    }
    

    • Proposed as answer by BoovendanM Tuesday, November 1, 2011 4:42 PM
    • Marked as answer by Bob Shen Monday, November 7, 2011 3:16 AM
    Tuesday, November 1, 2011 3:09 AM
  • Can you please post us the sample XML content.. So that it'll help us to know the schema better(else post the schema itself)..

    	XElement rawXML = XElement.Load(@"\SampleData.xml");
    	var aa = rawXML.Descendants("Chap");
    	foreach (var item in aa)
    	{
    		//item.Value to get the value of the Node(With this create the XML File)
    		//item.NextNode to get the Pair XML Content(Write this into that XML File)
    	}

    I've assumed the schema would be like,

    <Data>
      <Chap>1</Chap>
      <Pair>
        <Title>A</Title>
        <Description>Apple</Description>
      </Pair>
      <Chap>2</Chap>
      <Pair>
        <Title>A1</Title>
        <Description>Apple1</Description>
      </Pair>
      <Chap>3</Chap>
      <Pair>
        <Title>A2</Title>
        <Description>Apple2</Description>
      </Pair>
      <Chap>4</Chap>
      <Pair>
        <Title>A3</Title>
        <Description>Apple3</Description>
      </Pair>
    </Data>


    With Best Regards, Boovendan M
    • Edited by BoovendanM Monday, October 31, 2011 6:24 AM Code Added
    • Marked as answer by Bob Shen Monday, November 7, 2011 3:17 AM
    Monday, October 31, 2011 6:09 AM

All replies

  • Can you please post us the sample XML content.. So that it'll help us to know the schema better(else post the schema itself)..

    	XElement rawXML = XElement.Load(@"\SampleData.xml");
    	var aa = rawXML.Descendants("Chap");
    	foreach (var item in aa)
    	{
    		//item.Value to get the value of the Node(With this create the XML File)
    		//item.NextNode to get the Pair XML Content(Write this into that XML File)
    	}

    I've assumed the schema would be like,

    <Data>
      <Chap>1</Chap>
      <Pair>
        <Title>A</Title>
        <Description>Apple</Description>
      </Pair>
      <Chap>2</Chap>
      <Pair>
        <Title>A1</Title>
        <Description>Apple1</Description>
      </Pair>
      <Chap>3</Chap>
      <Pair>
        <Title>A2</Title>
        <Description>Apple2</Description>
      </Pair>
      <Chap>4</Chap>
      <Pair>
        <Title>A3</Title>
        <Description>Apple3</Description>
      </Pair>
    </Data>


    With Best Regards, Boovendan M
    • Edited by BoovendanM Monday, October 31, 2011 6:24 AM Code Added
    • Marked as answer by Bob Shen Monday, November 7, 2011 3:17 AM
    Monday, October 31, 2011 6:09 AM
  • the xml schema is:

    <concentration>
     <entry>
      <chapter>0</chapter>
      <pair>
        <term>Word1</term>
        <definition>Definition1</definition>
      </pair>
     </entry>
     <entry>
      <chapter>1</chapter>
      <pair>
        <term>Word2</term>
        <definition>Definition2</definition>
      </pair>
     </entry>
    </concentration>




     

    I used: 

     

    XmlTextReader reader = new XmlTextReader ("c:\\users/name/projects/csharp/concentrationgenerator/concentration001.xml");
    

     

    to read the xml doc into c#. now what i need to do is basically

     

    while <chapter> = 1 write everything within <pair> to chapter01.xml

    while <chapter> = 2 write everything within <pair> to chapter02.xml

     

    thanks in advance.

     

     



    Tuesday, November 1, 2011 2:40 AM
  • > what i need to do is read the doc, and every pair that has <chapter>1, write it to ch01.xml

    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    ...
    var xe = XElement.Load(@"c:\temp\concentration.xml");
    foreach(var c in xe.Descendants("chapter"))
    {
        var path = @"c:\temp\chapter" + c.Value.PadLeft(2, '0') + ".xml";
        using(var xw = new XmlTextWriter(path, Encoding.Default))
            c.NextNode.WriteTo(xw);
    }
    

    • Proposed as answer by BoovendanM Tuesday, November 1, 2011 4:42 PM
    • Marked as answer by Bob Shen Monday, November 7, 2011 3:16 AM
    Tuesday, November 1, 2011 3:09 AM
  • Hi ddrifmeyer84,

    How's it going? Do you have any updates about this issue?


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 3, 2011 7:29 AM