nawfal
nawfal

Reputation: 73293

How to read xml file up to a point so that I can write the same to another xml file later

A lot of similar threads are not exactly what I want or they deal with Linq. I am using .NET 2.0

I have an XML file which is more like a template. I need to write XML to another file, but I need it to refer to the template XML file for the initial formatting.

Like this:

My template file:

<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xml:lang="en">
  <head>
    <styling>
      <style id="s1" tts:fontFamily="SchoolHouse Cursive B" tts:fontSize="16" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="white" tts:backgroundColor="blue" tts:textAlign="center" />
      <style id="s2" tts:fontFamily="SchoolHouse Cursive B" tts:fontSize="16" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="yellow" tts:backgroundColor="black" tts:textAlign="center" />
   </styling>
  </head>
<body id="s1" style="s1">
  <div xml:lang="uk">
      <p begin="00:00:00" end="00:00:04">Hi! </p>
      <p begin etc............</p>
  </div>
 </body>
</tt>

I need to read the first part up to <p begin="00:00:00" end="00:00:04">Hi! </p> (not including that line). I need to get into any .NET type so that I can pass that to a function which will write them to another XML file.

Note: I dont want to read the template XML file node by node or element by element. I want a general solution as the initial part of the XML could be anything.

I tried:

XmlTextReader reader = new XmlTextReader(templateFile);
var list = new Something(); //what type would be ideal
while (reader.Read())
{
    if (reader.LocalName == "p")
       break;

    if (reader.LocalName == "")
       continue;

    list.Add(??); 
    //how do I read the rest here?
}

How will I be able to write the same?

Upvotes: 0

Views: 233

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273804

Seems like you want to use an XML file in a way that is not XML-valid. No XML reader or tool will let you work with the unclosed <body> and <div> tags.

I would consider treating it like a text file, but then you are relying on the line-break formatting in the source.

Drawback is that you will need to continue using it as Text when writing stuff, but I don't see a way around that.

Upvotes: 1

Related Questions