473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Nov 11 '05 #1
2 2861
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" <gu**@hotmail.com> wrote in message
news:#Q**************@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

Nov 11 '05 #2
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

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philippe Poulard | last post by:
hi, i have the following fragment : ****** <?xml version="1.0"> Begin <elem/> End ******
3
by: Alexander Mikhailian | last post by:
I have an http = new XMLHttpRequest(); that provides me with an http.responseXML. Somewhere deep in the http.responseXML there is a fragment called e.g. mydom that I want to copy with all its...
2
by: Rick Strahl [MVP] | last post by:
Hi all, I need a reality check <g>... I have the following XML fragment: <event sitename="West Wind Demo Link"> <url>http://rasnotebook/wconnect/testpage.wwd?Test2</url> <time>11/17/2003...
2
by: Yuriy | last post by:
Hi, any ideas how to read XML fragment from TextReader? XmlTextReader constructor accepts only Stream or string as source Do I miss something? thanks Yuriy
3
by: whapts3 | last post by:
I'm putting together a rather dynamic page (AJAX/all-that-mess). So... to go between different sorts of content on the page, I have it set up something like this. <a href="#10"...
3
by: Gustaf | last post by:
I'm trying to grasp this little passage from the XBRL spec: "The xlink:href attribute MUST be a URI. The URI MUST point to an XML document or to one or more XML fragments within an XML document....
0
by: Sky | last post by:
Hello: I'm trying to emulate the userSettings storage system on CE. XmlSerialization looks like a very useful 1-line solution that looks wonderful for serializing one class to one xml document...
2
by: lanegroups | last post by:
Dear experts, I'm trying to do the following using Xalan-C++ API 1. parse an XML document 2. evaluate an XML path 3. serialize the selected XML subtree 4. The new serialized XML fragment should...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.