XML Fragment into new XML Document

Hello All,

What's the most efficient way to do this?

I would like to load an xml file like this:

<?xml version="1.0" encoding="utf-8" ?>
<strings>
   <section key="1">
      <string>sdfa</string>
      <string>qwe</string>
   </section>
   <section key="2">
      <string>iouio</string>
     <string>kjhk</string>
   </section>
   <section key="3">
      <string>asjhjk</string>
     <string>mnwen</string>
   </section>
</strings>

and then based on the "key," create a new xml document (either a string or
XmlDocument) from a fragment of the full document like this:

<?xml version="1.0" encoding="utf-8" ?>
<strings>
   <section key="2">
      <string>iouio</string>
     <string>kjhk</string>
   </section>
</strings>

Thanks in advance,

GY


0
gu4y (2)
7/24/2003 6:42:44 PM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
571 Views

Similar Articles

[PageSpeed] 59

Here's what I've tried.

It works, but is it the most efficient?  (The actual documents are much
larger)

XmlDocument xmlSrc = new XmlDocument();
xmlSrc.Load("myfile.xml");

string xpath = "strings/section[@key='2']";
XmlNode nodeSrc = xmlSrc.SelectSingleNode(xpath);

XmlDocument xmlDest = new XmlDocument();
xmlDest.LoadXml("<strings></strings>");
XmlNode nodeDest = xmlDest.ImportNode(nodeSrc, true);
xmlDest.DocumentElement.AppendChild(nodeDest);





"gu4y" <gu4y@hotmail.com> wrote in message
news:#QnehNhUDHA.2224@TK2MSFTNGP10.phx.gbl...
> Hello All,
>
> What's the most efficient way to do this?
>
> I would like to load an xml file like this:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <strings>
>    <section key="1">
>       <string>sdfa</string>
>       <string>qwe</string>
>    </section>
>    <section key="2">
>       <string>iouio</string>
>      <string>kjhk</string>
>    </section>
>    <section key="3">
>       <string>asjhjk</string>
>      <string>mnwen</string>
>    </section>
> </strings>
>
> and then based on the "key," create a new xml document (either a string or
> XmlDocument) from a fragment of the full document like this:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <strings>
>    <section key="2">
>       <string>iouio</string>
>      <string>kjhk</string>
>    </section>
> </strings>
>
> Thanks in advance,
>
> GY
>
>


0
gu4y (2)
7/24/2003 7:48:50 PM
gu4y wrote:

> It works, but is it the most efficient?  (The actual documents are much
> larger)
If you are talking about effectiveness, try to avoid using XmlDocument and 
XPathDocument, because they load the whole XML document into the memory. In 
your case it's possible to get the job done by simple XmlReader/XmlWriter filter:
namespace Test2 {
     class Test {
         static void Main(string[] args) {
             string xml =
             @"<strings>
                     <section key=""1"">
                         <string>sdfa</string>
                         <string>qwe</string>
                     </section>
                     <section key=""2"">
                         <string>iouio</string>
                         <string>kjhk</string>
                     </section>
                     <section key=""3"">
                         <string>asjhjk</string>
                         <string>mnwen</string>
                     </section>
                </strings>";
             XmlReader reader = new XmlTextReader(new StringReader(xml)); 

             XmlWriter writer = new XmlTextWriter(Console.Out);
             while (reader.Read()) {
                 if (reader.NodeType == XmlNodeType.Element && reader.Name == 
"strings") {
                     writer.WriteStartElement(reader.Name);
                 }
                 if (reader.NodeType == XmlNodeType.Element && reader.Name == 
"section" &&
                     reader["key"]=="2") {
                     writer.WriteNode(reader, true);
                 }
             }
         }
     }
}

-- 
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

0
oleg7603 (294)
7/27/2003 10:18:36 AM
Reply:

Similar Artilces:

New Quote
I am trying to set up a quote in MSCRM but the £ field is grayed out, i do not seem to be able to set up a manuel quote without setting up a price list? Can anybody help, do not seem to be able to find answers in the help file or i am asking the wrong questions Microsoft CRM v1.2 Quote, Order, Invoice £ fields are all dependent on price list/product selection - system calculated based on your product selection. Frank Lee Workopia, Inc. http://www.workopia.com/Links.htm "Raphael" wrote: > I am trying to set up a quote in MSCRM but the £ field is grayed ou...

Importing Purchase order documents
Hello All Quick! I need a reliable tool to import PO documents into GP. I've just tried table import and it doesnt work so I need an alternative suggestion. Its non-existant in IM even they MS went to the trouble to building the data sources. Please help! Regards & thanks in advance Louella, Please drop an email to discuss. MV nj.tech@hotmail.com "Louella" wrote: > Hello All > > Quick! I need a reliable tool to import PO documents into GP. I've just > tried table import and it doesnt work so I need an alternative suggestion...

New Mail Sound Notification
Hi The first time my Outlook 2003 does a send/receive, if I have mail there is no Sound notification. Second time, and onwards, it's fine. Not the end of the world, but annoying! Any ideas? TIA Nik. -- ==================================================================== Remove S P A M K I L L From my Email address to reply to me Directly ==================================================================== P4 3.2 / 512MB Samsung 400mhz PC3200 RAM/ 200GB Maxtor SATA / HP DJ5550 Sony DVD RW / Sony CD/RW / Logitech MX700 KB+Mse / Viewsonic VE710b TFT Nvidia GF FX57...

New on-line services at institution but Money 2004 doesn't show th
Fairly recently my credit card issuer added online account download services. I have gone to the institution and used it to download a qif file. But when I click on this institutions name in the list presented by on-line services manager it doesn't show any services. (The name is in the list.) Is there a process where the list is updated with newly offered services? If so, how long should it take for a new one to appear. Or is there someone I can notify with the name of the institution who can then add the service. Thanks -- Harris S. Freeman Alliance Strategies and Programs ...

Exch2003: unable to create new mailboxes after replacing corrupt info store
I had a corrupt information store on Exchange 2003 so I added a new temporary server (Exch2003) and moved all of my mailboxes to the new server. After I did that, I saved the copy of the original info store to another location and created a new info store with the same name. Then I moved all of the mailboxes back to the "new" info store. Everything runs great - backups, log files, email flow except for - - creating a new user/mailbox. The user part creates fine and it looks like a mailbox is created, but looking under the mailbox folder doesn't show a mailbox for th...

XML Header
Hi, I have some XML doc loaded in a C# Web Service. XmlDocument doc = new XmlDocument(); doc.LoadXml(myDoc); Code Missing to manipulate header (I would like to replace or manupulate all these XXXXX): <mso-XXXXXX productversion="XXXXXX"> <mso-application progid="XXXXX"?> doc.Save(myNewDoc); Any idea on how to access and manipulate data within the doc header? Thanks vbdev vb6dev2003 wrote: > I have some XML doc loaded in a C# Web Service. > XmlDocument doc = new XmlDocument(); > doc.LoadXml(myDoc); > > Code Missing to manipulate header ...

Read XHTML into XML
Hi all, I need to read/parse XHTML aspx pages and look for certain tokens and content. How can I use a XmlTextReader for this? If not, any other ideas? Thanks in advance, JA Reyes. Jose Antonio Reyes wrote: > I need to read/parse XHTML aspx pages and look for certain tokens and > content. How can I use a XmlTextReader for this? If not, any other ideas? If the pages are well-formed XHTML then it is possible to use XmlReader (in .NET 2.0/3.0) or XmlTextReader (in .NET 1.x) to parse the XHTML documents. You can also use the other XML APIs .NET provides so using XPathNavigator and/o...

The virtual memory necessary to run your Exchange server is fragmented
I get the following error on a daily basis... The virtual memory necessary to run your Exchange server is fragmented in such a way that normal operation may begin to fail. It is highly recommended that you restart all Exchange services to correct this issue. I have exchange 2000, service pack 3. I have failed to get any workable answers. Any ideas? http://support.microsoft.com/default.aspx?scid=kb;en-us;325044 -- Bharat Suneja MCSE, MCT -------------------------------- "asdfg" <asda@asda.com> wrote in message news:eXfxsVfvFHA.2504@TK2MSFTNGP15.phx.gbl... >I get t...

new problem
Ok thanks for all the help on my previous questions but here's a new one still related to timesheets. My admin staff is paid hourly/salary 40hrs. Column c5:c9 is Time In, d5:d9 is Time out. I've formatted the cells TIME and chose the 1:30 PM example. When i type in 8 automatically it changes to 12:00 AM. If I put 8:00 then it changes to 8:00 AM, now in the D cells i want it to read 4:00 PM. How do i get it to read AM vs PM? Hi why not use a 24 hour format :-) In D1 enter 4:00 PM -- Regards Frank Kabel Frankfurt, Germany Lise wrote: > Ok thanks for all the help o...

Parsing Excel Documents
I have a number of Excel documents that I would like to extract data from. The data however is no in a CSV layout. There are some rules on how the file is laid out (for example the data rows i want are always after the first empty row after the first lot of data in the file). I wanted to know if there is an exensible consumer of Excel Docuemnts that can be used in an MFC app. I'll be using .NET studio 2002 and the code will be UNmanaged. Thanks in advance. MattC >I wanted to know if there is an exensible consumer of Excel Docuemnts that >can be used in an MFC app. Matt, You...

How to invoke the xml editor?
I installed vs2005 on a new xp pro machine, and installed everything except C++. When I click on a file with a ".xml" extension it is opened with IE. When I click file/Edit with XML Editor, it opens it with Notepad. If I click File/Open With and select XML Editor, it opens it with IE. So, where is the xml editor? Can it be invoked from withing VS? Where? Bill * billsahiker@yahoo.com wrote in microsoft.public.dotnet.xml: >I installed vs2005 on a new xp pro machine, and installed everything >except C++. When I click on a file with a ".xml" extension it is >opened ...

How can I go to a specific page number in a large document
How do I go to a specific page number in a large word document. Read the replies to your earlier identical post -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> "yisaajao" <yisaajao@discussions.microsoft.com> wrote in message news:CFC4289D-FC93-4C1B-951B-DB08CED833...

show XML schema and values
I have an XML document that looks like the following: <record> <Field1>value1</Field1> <Field2>value2</Field2> <Field3>value3</Field3> </record> What I'm aiming to do is to show the data in a report as in the following example: record: Field1 value1 Field2 value2 Field3 value3 The "Field1-3"-elements can have any name, so I'm trying to get a table that has in one result column the element name, and in the second colum...

Installing Money 2003 on new PC..what files to copy from old PC?
I have purchased a new PC (Windows XP). To move Money 2003 to the new one, I assume that I would just install Money from the original CD. My question is, what files do I need to copy from my old PC? (old PC was Windows 98) Thanks, Tommy Flynn Your .MNY file(s). And you will need to move most of the Money Tools|Options settings by hand. Worst case for these is printer check alignment settings. "Tommy Flynn" <tflynn@mindspring.com> wrote in message news:bfbff0$fjf$1@slb9.atl.mindspring.net... > I have purchased a new PC (Windows XP). To move Money 2003 to the new one...

Adding new worksheets to an array
I'm in the middle of a project to compare like items from differen vendors. I have set this up so that each vendor gets a separat worksheet for entering prices (that need to be updated regularly)tha is named for the vendor when the name is typed in at the top of th sheet. I have another page where all the pricing on like items fro different vendors is compared and the lowest price on an item i displayed and which vendor has the lowest price My issue is that when I add a vendor I have to go in and manually buil all the links to the new sheet. Is there a way I can have a newl named sh...

Routing Documents for Review
Hello! When I choose "send to" > "mail recipients for review" the document becomes an attachement to the email and I would like to have a link to the document where it is saved on our network. Please help me be able to change this default so that a link is provided instead of the attachement, or even better...how can I get prompted to choose link or attach the file in case I want to send outside of our network....? TIA for your help! Raymond Hi Raymond- Your request suggests that you are at crossed purposes. The point of "for Review" is so the re...

Printing document references
Hello, We have a number of forms which we have produced here at work which require numbered references on them so each document is unique. Can this be batch driven ? EG. I have designed a printed sheet in Excel for our stock take this year. We need 200 sheets to be issued out to the guys doing the counting. Each sheet needs its own unique documents reference number on it so it can be traced. How can I automate this so I can print 200 copies of the document, and the document references increase incrementally on each separate page. EG. ABC0001, ABC0002, ABC0003... I dont want to have t...

Offline synch using XML web-services
This is a bit long-winded. Can somebody read thru these 2 use cases, and tell me if it is feasible ? :- Use Case : Replication before Offline Synch Scenario 1 : Oracle Server , MSDE client 1. User clicks on 'Go Offline'. System creates a MSDE SQL database in laptop (client), gets the schemas of the tables in Oracle, creates corresponding tables in client's MSDE SQL (mapping Oracle datatypes to SQL datatypes). 2. Offline replication program reads in the tables of the server into a typed datasets, converts the typed datasets into XML, and stores the XML in the client. (XML serial...

XML Serialization ... how to truncate <?xml...
While xml serializing object how can I truncate <?xml ...?> this line alone? I need to send the serialized xml data to a web method ... since SOAP already have <?xml tag there was some confusion Any help would be greatly appreciated Thank you Regards Raj Raj wrote: > While xml serializing object how can I truncate <?xml ...?> this line alone? Serialize to an XmlWriter created with XmlWriterSettings where you set OmitXmlDeclaration to true. -- Martin Honnen --- MVP XML http://msmvps.com/blogs/martin_honnen/ ...

Moving to new computer
I'm running Money 2008 and recently added a new computer to my home network. I want to run Money on the new computer instead of the older one. My plan is to move the Money database to the new computer, install Money on the new computer using the same installation disk I used before on the older computer and then use the new install to open the moved database on the new computer. I do NOT want to continue running Money on the older computer and will uninstall it when I'm sure everything is OK on the new computer. Should that work OK? Will I still be legally licensed and get Mo...

Moving CRM to new server in same domain
We're moving our CRM 3.0 server to a new one, but it will still be in the same domain. If the SQL DB's are another server, I'm assuming all we need to do is the following? 1. Export all customizations and workflows 2. Stop Workflow services 3. On new server, run CRM Redeployment tool and point to existing DB's. 4. On new server, import customizations and workflows. Please correct me if I'm missing something here... Also, what happens to all the custom reports on the CRM server? Do those get moved with the redeployment tool as well? Thanks in Advance! Hi, If y...

String to xml document
Hi there, I was hoping someone can help me with a little problem I can't seem to find any answers to. I've got a form wich will be posted. On receiving the post I would like to parse a formvar to a xml document. The contents of this formvar (string) will be a valid xhtml document. Can someone give me a startertip (or more :-)) on how to achieve this? tnx in advance, James van der Veen Sjeems wrote: > I was hoping someone can help me with a little problem I can't seem to find > any answers to. > > I've got a form wich will be posted. On receiving the p...

error adding new Schema Field 04-21-04
Hi, When I attempt to add a new schema field to the "Lead" object in the Deployment Manager, I get the following error: "An error occured during the addtion of the new field. The addition failed." In the event log, I there are two relevant entries: "dmLog: Failed to add new Picklist attribute (CFPsolution) to Lead entity" and "dmLog: sp_repladdcolumn failed. - Timeout expired" I've previously successfully added a pickup list called "Sector" with 9 values. Therefore I don't think I've exceeded the 8000 char row limit. Apart from...

Setting up new Exchange Server
I have a relatively small, simple, and pristine 2003 AD Domain running. We currently use POP3 e-mail accounts from our ISP but we're planning to deploy Exchange 2003 (Standard). All of the systems on our network use private addressing but we have a range of public IP's that we plan to use for web and mail sites/servers. We only have around 40 users and want to setup OWA with Exchange but we don't have the luxery of multiple servers right now. We also plan on setting up other web sites. I understand from reading other threads that it isn't best to setup the Exchange server ...

Version of Publisher document
Is there a way to know the version of a Publisher document? Also, I have a few documents converted from 2000 to 2002 which will not open in 2003. Any ideas? Thanks. There should be no reason why you cannot open older versions of Publisher documents in 2003. Do you have Norton? How to use Office programs with the Norton AntiVirus Office plug-in http://support.microsoft.com/kb/329820/en-us A updated security fix has prevented Publisher 2003 from opening 2.0 publications. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com http://offi...