Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Need help creating another XML file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mary Smith - 07 Jan 2008 03:40 GMT
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
Arne Vajhøj - 07 Jan 2008 03:53 GMT
> 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
[quoted text clipped - 71 lines]
>  
> 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
sloan - 07 Jan 2008 04:50 GMT
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
Mr. Arnold - 07 Jan 2008 05:08 GMT
The link is in VB, but you should be able to figure it out. You should look
at the XMLDocument read section.

http://developer.yahoo.com/dotnet/howto-xml_vb.html

You should be able to look at the two links and figure out how to read the
XML and how to write out the XML that you need using XML Documents.

http://support.softartisans.com/kbview_675.aspx

I have been into XML here recently on a project. I now know more about XML
and  SOAP XML Web Services than I ever even cared to know. :)
Marc Gravell - 07 Jan 2008 05:27 GMT
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
Mary Smith - 07 Jan 2008 12:49 GMT
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
Marc Gravell - 07 Jan 2008 13:33 GMT
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:output 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>
sloan - 07 Jan 2008 14:12 GMT
I concur, and that is what my above post suggests.

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

> Can I offer something? Perhaps try the xslt route first... very simple
> and easy to maintain... and not as memory hungry as XmlDocument.
[quoted text clipped - 43 lines]
>   </xsl:template>
> </xsl:stylesheet>
Mary Smith - 07 Jan 2008 15:15 GMT
Thank you this was exactly what I needed. The output is very fast.

Thanks again
Mary

> Can I offer something? Perhaps try the xslt route first... very simple and
> easy to maintain... and not as memory hungry as XmlDocument.
[quoted text clipped - 43 lines]
>  </xsl:template>
> </xsl:stylesheet>
Marc Gravell - 07 Jan 2008 15:28 GMT
No problem; for maintenance/explanatory purposes, MSDN has an xsl
reference here:
http://msdn2.microsoft.com/en-us/library/ms256069.aspx

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.