Can we import XML file into another XML file?

XmlFileImportMerge

Xml Problem Overview


Can we import an XML file into another XML file?

I mean is there any import tag in XML that takes XML path as parameter and imports XML (for which path is provided).

Xml Solutions


Solution 1 - Xml

You could use an [external (parsed) general entity][1].

You declare the entity like this:

<!ENTITY otherFile SYSTEM "otherFile.xml">

Then you reference it like this:

&otherFile;

A complete example:

<?xml version="1.0" standalone="no" ?>
<!DOCTYPE doc [
<!ENTITY otherFile SYSTEM "otherFile.xml">
]>
<doc>
  <foo>
    <bar>&otherFile;</bar>
  </foo>
</doc>

When the XML parser reads the file, it will expand the entity reference and include the referenced XML file as part of the content.

If the "otherFile.xml" contained: <baz>this is my content</baz>

Then the XML would be evaluated and "seen" by an XML parser as:

<?xml version="1.0" standalone="no" ?>
<doc>
  <foo>
    <bar><baz>this is my content</baz></bar>
  </foo>
</doc>

A few references that might be helpful:

Solution 2 - Xml

The other answers cover the 2 most common approaches, Xinclude and XML external entities. Microsoft has a really great writeup on why one should prefer Xinclude, as well as several example implementations. I've quoted the comparison below:

Per http://msdn.microsoft.com/en-us/library/aa302291.aspx

> Why XInclude? > > The first question one may ask is "Why use XInclude instead of XML > external entities?" The answer is that XML external entities have a > number of well-known limitations and inconvenient implications, which > effectively prevent them from being a general-purpose inclusion > facility. Specifically: > > - An XML external entity cannot be a full-blown independent XML document—neither standalone XML declaration nor Doctype declaration is > allowed. That effectively means an XML external entity itself cannot > include other external entities. > - An XML external entity must be well formed XML (not so bad at first glance, but imagine you want to include sample C# code into your XML > document). > - Failure to load an external entity is a fatal error; any recovery is strictly forbidden. > - Only the whole external entity may be included, there is no way to include only a portion of a document. > -External entities must be declared in a DTD or an internal subset. This opens a Pandora's Box full of implications, such as the fact that > the document element must be named in Doctype declaration and that > validating readers may require that the full content model of the > document be defined in DTD among others. > > The deficiencies of using XML external entities as an inclusion > mechanism have been known for some time and in fact spawned the > submission of the XML Inclusion Proposal to the W3C in 1999 by > Microsoft and IBM. The proposal defined a processing model and syntax > for a general-purpose XML inclusion facility. > > Four years later, version 1.0 of the XML Inclusions, also known as > Xinclude, is a Candidate Recommendation, which means that the W3C > believes that it has been widely reviewed and satisfies the basic > technical problems it set out to solve, but is not yet a full > recommendation.

Another good site which provides a variety of example implementations is https://www.xml.com/pub/a/2002/07/31/xinclude.html. Below is a common use case example from their site:

<book xmlns:xi="http://www.w3.org/2001/XInclude">

  <title>The Wit and Wisdom of George W. Bush</title>

  <xi:include href="malapropisms.xml"/>

  <xi:include href="mispronunciations.xml"/>

  <xi:include href="madeupwords.xml"/>

</book>

Solution 3 - Xml

Solution 4 - Xml

Mads Hansen's solution is good but to succeed in reading the external file in .NET 4 took some time to figure out using hints in the comments about resolvers, ProhibitDTD and so on.

This is how it's done:

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Parse;
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
        settings.XmlResolver = resolver;
        var reader = XmlReader.Create("logfile.xml", settings);
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        foreach (XmlElement element in doc.SelectNodes("//event"))
        {
            var ch = element.ChildNodes;
            var count = ch.Count;
        }

logfile.xml:

<?xml version="1.0"?>
<!DOCTYPE logfile [
<!ENTITY events    
 SYSTEM "events.txt">
]>
<logfile>
&events;
</logfile>

events.txt:

<event>
    <item1>item1</item1>
    <item2>item2</item2>
</event>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSumitView Question on Stackoverflow
Solution 1 - XmlMads HansenView Answer on Stackoverflow
Solution 2 - XmlVoteCoffeeView Answer on Stackoverflow
Solution 3 - XmlTomasz NurkiewiczView Answer on Stackoverflow
Solution 4 - Xmluser3717478View Answer on Stackoverflow