5

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!

Slauma
  • 175,098
  • 59
  • 401
  • 420

3 Answers3

3

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I was just searching and experimenting a bit with `XInclude` (http://en.wikipedia.org/wiki/XInclude and http://www.w3.org/TR/xinclude/) but I didn't get this working. When I reference the XInclude namespace the `xi:include` element isn't recognized (in VS2008). I'm wondering if this is only a W3C "recommendation" which has never been implemented by anyone. Or perhaps am I interpreting the purpose of XInclude wrong? – Slauma Dec 04 '10 at 13:12
  • Finally I have introduced my own "Include" element and processed the whole XML document without the XmlSerializer. Thanks for the tip! – Slauma Dec 05 '10 at 23:08
0

The answer to the first question is Yes. You can do an xInclude to get the content included in the master file (http://www.w3.org/TR/xinclude/)

  • It's not that easy. `xinclude` is a w3c *recommendation* that isn't widely implemented. (That's probably why I didn't get it working, see my comment under the accepted answer.) For example no browser supports it and also no support in .NET is available where you have to rely on third party tools and extensions: http://stackoverflow.com/questions/14809487/alternative-to-xinclude – Slauma Jul 07 '13 at 13:18
0

If you can modify your schema, you might be able to use XML entities.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56