Need help creating another XML file

M

Mary Smith

I want to create a new XML file from another XML file. The original file will be stored on the hard drive and it is about 3mb. I do not need everything from the original XML file so the new one will be much smaller and easier to manage. I only need to do this when new data has been added to the original XML file. Here is a sample of what I have.

Original XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast>
<star>
<person>
<displayname>Daniel Craig</displayname>
</person>
<character>James Bond</character>
</star>
<star>
<person>
<displayname>Eva Green</displayname>
</person>
<character>Vesper Lynd</character>
</star>
</cast>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast>
<star>
<person>
<displayname>Tobey Maguire</displayname>
</person>
<character>Spider-Man/Peter Parker</character>
</star>
<star>
<person>
<displayname>Kirsten Dunst</displayname>
</person>
<character>Mary Jane Watson</character>
</star>
<star>
<person>
<displayname>Willem Dafoe</displayname>
</person>
<character>Green Goblin/Norman Osborn</character>
</star>
</cast>
</movie>
</movielist>

Below is how I want the new file to look by combining the displayname and character in the cast section.

New XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast1>Daniel Craig - James Bond</cast1>
<cast2>Eva Green - Vesper Lynd</cast2>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast1>Tobey Maguire - Spider-Man/Peter Parker</cast1>
<cast2>Kirsten Dunst - Mary Jane Watson</cast2>
<cast3>Willem Dafoe - Green Goblin/Norman Osborn</cast3>
</movie>
</movielist>

Any help will be greatly appreciated

Thanks
Mary
 
A

Arne Vajhøj

Mary said:
I want to create a new XML file from another XML file. The original file
will be stored on the hard drive and it is about 3mb. I do not need
everything from the original XML file so the new one will be much
smaller and easier to manage. I only need to do this when new data has
been added to the original XML file. Here is a sample of what I have.

Original XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast>
<star>
<person>
<displayname>Daniel Craig</displayname>
</person>
<character>James Bond</character>
</star>
<star>
<person>
<displayname>Eva Green</displayname>
</person>
<character>Vesper Lynd</character>
</star>
</cast>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast>
<star>
<person>
<displayname>Tobey Maguire</displayname>
</person>
<character>Spider-Man/Peter Parker</character>
</star>
<star>
<person>
<displayname>Kirsten Dunst</displayname>
</person>
<character>Mary Jane Watson</character>
</star>
<star>
<person>
<displayname>Willem Dafoe</displayname>
</person>
<character>Green Goblin/Norman Osborn</character>
</star>
</cast>
</movie>
</movielist>

*Below is how I want the new file to look by combining the displayname
and character in the cast section.*

New XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast1>Daniel Craig - James Bond</cast1>
<cast2>Eva Green - Vesper Lynd</cast2>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast1>Tobey Maguire - Spider-Man/Peter Parker</cast1>
<cast2>Kirsten Dunst - Mary Jane Watson</cast2>
<cast3>Willem Dafoe - Green Goblin/Norman Osborn</cast3>
</movie>
</movielist>

Any help will be greatly appreciated

You read the old XML into an XmlDocument and either:
1) iterate through it via C# code and construct a new
XmlDocument with what you want and store that to disk
or:
2) use XSLT to transform it to the new XML you want

Arne
 
S

sloan

This should be exactly what you're looking for:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!148.entry


I want to create a new XML file from another XML file. The original file will be stored on the hard drive and it is about 3mb. I do not need everything from the original XML file so the new one will be much smaller and easier to manage. I only need to do this when new data has been added to the original XML file. Here is a sample of what I have.

Original XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast>
<star>
<person>
<displayname>Daniel Craig</displayname>
</person>
<character>James Bond</character>
</star>
<star>
<person>
<displayname>Eva Green</displayname>
</person>
<character>Vesper Lynd</character>
</star>
</cast>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast>
<star>
<person>
<displayname>Tobey Maguire</displayname>
</person>
<character>Spider-Man/Peter Parker</character>
</star>
<star>
<person>
<displayname>Kirsten Dunst</displayname>
</person>
<character>Mary Jane Watson</character>
</star>
<star>
<person>
<displayname>Willem Dafoe</displayname>
</person>
<character>Green Goblin/Norman Osborn</character>
</star>
</cast>
</movie>
</movielist>

Below is how I want the new file to look by combining the displayname and character in the cast section.

New XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast1>Daniel Craig - James Bond</cast1>
<cast2>Eva Green - Vesper Lynd</cast2>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast1>Tobey Maguire - Spider-Man/Peter Parker</cast1>
<cast2>Kirsten Dunst - Mary Jane Watson</cast2>
<cast3>Willem Dafoe - Green Goblin/Norman Osborn</cast3>
</movie>
</movielist>

Any help will be greatly appreciated

Thanks
Mary
 
M

Mr. Arnold

M

Marc Gravell

Other posts have mentioned the options... just to mention that DOM
models (such as XmlDocument) can (depending on the data) take
significantly more memory that the original data-file, which might be
worth considering since your original file is not trivial (3Mb).

XmlDocument presents a simple model, so perhaps just benchmark it?
Load the data into XmlDocument (.Load) and see how the memory usage
jumps? If it is significant, XmlReader and XmlWriter might be a good
way to go.

This would also be a simple xslt operation, which could be useful as I
believe this uses (if given a stream or XmlReader) the more light-
weight readonly DOM model (the class name escapes me at the moment...)

Marc
 
M

Mary Smith

What will be the best way to change the cast section?

OLD XML
<cast>
<star>
<person>
<displayname>Daniel Craig</displayname>
</person>
<character>James Bond</character>
</star>
<star>
<person>
<displayname>Eva Green</displayname>
</person>
<character>Vesper Lynd</character>
</star>
</cast>

NEW XML
<cast1>Daniel Craig - James Bond</cast1>
<cast2>Eva Green - Vesper Lynd</cast2>

I am currently using the following code.

XmlDocument doc = new XmlDocument();
doc.Load("old.xml");
XmlTextWriter writer = new XmlTextWriter("new.xml", null);
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("movielist");

XmlNodeList nodes = doc.SelectNodes("movielist/movie");

foreach (XmlNode node in nodes)
{
writer.WriteStartElement("movie");

XmlNode titleNode = node.SelectSingleNode("title");
writer.WriteElementString("title", titleNode.InnerText);

XmlNode indexNode = node.SelectSingleNode("index");
writer.WriteElementString("index", indexNode.InnerText);
}

writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();

Thanks for the replies
Mary


I want to create a new XML file from another XML file. The original file will be stored on the hard drive and it is about 3mb. I do not need everything from the original XML file so the new one will be much smaller and easier to manage. I only need to do this when new data has been added to the original XML file. Here is a sample of what I have.

Original XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast>
<star>
<person>
<displayname>Daniel Craig</displayname>
</person>
<character>James Bond</character>
</star>
<star>
<person>
<displayname>Eva Green</displayname>
</person>
<character>Vesper Lynd</character>
</star>
</cast>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast>
<star>
<person>
<displayname>Tobey Maguire</displayname>
</person>
<character>Spider-Man/Peter Parker</character>
</star>
<star>
<person>
<displayname>Kirsten Dunst</displayname>
</person>
<character>Mary Jane Watson</character>
</star>
<star>
<person>
<displayname>Willem Dafoe</displayname>
</person>
<character>Green Goblin/Norman Osborn</character>
</star>
</cast>
</movie>
</movielist>

Below is how I want the new file to look by combining the displayname and character in the cast section.

New XML file

<movielist>
<movie>
<index>1</index>
<title>Casino Royale</title>
<cast1>Daniel Craig - James Bond</cast1>
<cast2>Eva Green - Vesper Lynd</cast2>
</movie>
<movie>
<index>2</index>
<title>Spider-Man</title>
<cast1>Tobey Maguire - Spider-Man/Peter Parker</cast1>
<cast2>Kirsten Dunst - Mary Jane Watson</cast2>
<cast3>Willem Dafoe - Green Goblin/Norman Osborn</cast3>
</movie>
</movielist>

Any help will be greatly appreciated

Thanks
Mary
 
M

Marc Gravell

Can I offer something? Perhaps try the xslt route first... very simple
and easy to maintain... and not as memory hungry as XmlDocument.

The necessary xslt file follows; the code to invoke it (via C#) is as
simple as:

==== program.cs ====

using System;
using System.Xml.Xsl;
class Program {
static void Main() {
// cache and re-use as necessary
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("transform.xslt");
// invoke
xslt.Transform("movies.xml", "out.xml");
}
}

==== transform.xslt ====

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">

<xsl:blush:utput method="xml" indent="yes"/>
<xsl:template match="movielist">
<movielist>
<xsl:apply-templates select="movie"/>
</movielist>
</xsl:template>
<xsl:template match="movie">
<movie>
<index><xsl:value-of select="index"/></index>
<title><xsl:value-of select="title"/></title>
<xsl:for-each select="cast/star">
<xsl:element name="cast{position()}">
<xsl:value-of select="concat(person/displayname,' -
',character)"/>
</xsl:element>
</xsl:for-each>
</movie>
</xsl:template>
</xsl:stylesheet>
 
S

sloan

I concur, and that is what my above post suggests.

Try xsl. Its MUCH easier to maintain....and will probably perform better.
 
M

Mary Smith

Thank you this was exactly what I needed. The output is very fast.

Thanks again
Mary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top