How can I “include” one XML file into another XML file?

I have a XML schema like:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="MySchema"
    targetNamespace="http://tempuri.org/MySchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/MySchema.xsd"
    xmlns:mstns="http://tempuri.org/MySchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="MyDocument">
    <xs:complexType>
      <xs:all>
        <xs:element name="TextHeader" type="xs:string" minOccurs="0" />
        <xs:element name="TextBody" type="xs:string" minOccurs="0" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

A valid XML document according to this schema would be:

<?xml version="1.0" encoding="utf-8" ?>
<MyDocument xmlns="http://tempuri.org/MySchema.xsd">
  <TextHeader>My header which is almost always the same...</TextHeader>
  <TextBody>My text body which is always different...</TextBody>
</MyDocument>

Question 1: Is there a way to "include" the TextHeader element from another file?

Like so:

File "Header.xml":

<?xml version="1.0" encoding="utf-8" ?>
<MyDocument xmlns="http://tempuri.org/MySchema.xsd">
  <TextHeader>My Header which is almost always the same...</TextHeader>
</MyDocument>

File "CompleteDocument.xml":

<?xml version="1.0" encoding="utf-8" ?>
<MyDocument xmlns="http://tempuri.org/MySchema.xsd">

  include "Header.xml" ???

  <TextBody>My text body which is always different...</TextBody>
</MyDocument>

Question 2: If it's possible at all, will the .NET XMLSerializer be able to parse and understand the document containing such an "include..."?

Thank you for help in advance!

Answers


No to both questions. You could code something (like how app.config allows sections to be imported, or how xslt handles includes/imports), but this is not inbuilt onto any XML spec and would not match the existing schema. You'd have to do everything yourself, basically.

If you really wanted to go this route, writing a custom XmlReader that recognised a specific element (in a specific xmlns) and silently merging at that point - would perhaps be the beat choice.


Need Your Help

Gibberish is returned when runnign fopen/file_get_contents with one server but not with another

php curl character-encoding fopen file-get-contents

I'm building a script to check whether sites are up or not by reading a given page's content and looking for a predefined string in it (if the site is down the string won't be found).