views:

168

answers:

2

I have some xml data contained in three files (Database.xml, Participants.xml, and ConditionTokens.xml). I am trying to use external entities to place the participants and condition tokens into the database file, but when I run this code...

string xmlPath = Environment.CurrentDirectory + @"\Data\Database.xml";
XElement database = XElement.Load(xmlPath);

...there are no participants or condition tokens in my xml (the HasElements property for "database" is false). There should be two child elements. I get no errors/warnings within Visual Studio (2008), and the live schema validation seems to be happy, but something is not quite right when I run my code.

Could anyone tell me what I'm doing wrong?

I have pasted the three xml files below.

Thanks very much!

-Dan

Database.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE database [
  <!ENTITY conditionTokens SYSTEM "ConditionTokens.xml">
  <!ENTITY participants SYSTEM "Participants.xml">]>
<database
  xmlns="experimentManager"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="experimentManager Database.xsd">
  &conditionTokens;
  &participants;
</database>

ConditionTokens.xml

<?xml version="1.0" encoding="utf-8" ?>
<conditionTokens>
  <conditionToken>
    <id>1</id>
    <token>LargeToSmall</token>
  </conditionToken>
  <conditionToken>
    <id>2</id>
    <token>SmallToLarge</token>
  </conditionToken>
</conditionTokens>

Participants.xml

<?xml version="1.0" encoding="utf-8" ?>
<participants>
  <participant>
    <id>1</id>
    <conditionTokenId>1</conditionTokenId>
  </participant>
  <participant>
    <id>2</id>
    <conditionTokenId>2</conditionTokenId>
  </participant>
</participants>
A: 

I would have used the XmlDocument class to load the 3 documents

XmlDocument xmlDatabase = new XmlDocument();
xmlDatabase.Load(databasePath);
XmlDocument xmlTokens = new XmlDocument();
xmlTokens.Load(tokensPath);
XmlDocument xmlParticipants = new XmlDocument();
xmlParticipants.Load(participantsPath);

Then using the ImportNode and AppendNode attach then to each other...

xmlDatabase.FirstChild.AppendNode(xmlDatabase.ImportNode(xmlTokens.FirstChild), true);
xmlDatabase.FirstChild.AppendNode(xmlDatabase.ImportNode(xmlParticipants.FirstChild), true);

That should pretty much do it (or instead of using FirstChild use an xpath selector?)

d1k_is
That's a good idea, but the one downside is that I would lose Visual Studio's live validation checking. Ideally, I'd like to have separate files but have them validated as if they were a single file. If I can't get it to work with the !ENTITY statements, though, I'll go with what you suggest.Thanks!-Dan
DanM
A: 

I ended up using <xs:redefine> instead.

DanM
Dan, I have the same issue. Can you share 3 XML file details with xs:redefine tag?
@suneelk, I can't dig these up easily, but have you checked out http://www.w3schools.com/schema/el_redefine.asp? If you have any specific questions, I might be able to help.
DanM