473,241 Members | 1,508 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,241 software developers and data experts.

Insert Doctype Node Dynamically

I am receiving xml documents from a customer without a reference to a
doctype. I know what the Doctype DTD should be need to insert the
declaration as follows

<?xml version="1.0" encoding="UTF-8"?>
<!-- start of add validation tag -->
<!DOCTYPE D1XML SYSTEM "http://url/myDTD.DTD">
<!-- end of add validation tag -->
<XML.....>

Using one of the provided framework classes such as XmlValidatingReader,
etc.... is there a method to dynamically add a doctype reference? Or do I
have to find the starting node and perform an insert to the doctype
afterwards? Bottom line is I need a foolproof way to add this doctype
reference in the right spot each time.

For all of you asking, "Why...." The customer will be adding this DTD
reference shortly, but for now this is the "band-aid" that needs to be
applied.

Thanks for your help.
Nov 12 '05 #1
1 4081
"Chris Fink" <ch********@gmail.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
I am receiving xml documents from a customer without a reference to a
doctype. I know what the Doctype DTD should be need to insert the
declaration as follows

<?xml version="1.0" encoding="UTF-8"?>
<!-- start of add validation tag -->
<!DOCTYPE D1XML SYSTEM "http://url/myDTD.DTD">
<!-- end of add validation tag -->
<XML.....>

Using one of the provided framework classes such as XmlValidatingReader,
etc.... is there a method to dynamically add a doctype reference?


While streaming it through an XmlValidatingReader, one possibility
is to subclass one of the Reader classes from the System.IO name-
space to insert the additional line(s) you seek for the reference to
an external DTD.

Here is an example of such a class that could do this (I only impl'd
the Read(char[],int,int) method because in a quick test, that's what
was used when loading an XmlDocument as you may if you wrap
this Reader in an XmlTextReader -- so your mileage may vary.)

- - - InsertLineStreamReader.cs (partial)
using System;
using System.Collections;
using System.IO;

namespace Derek.IO
{
public class InsertLineStreamReader : System.IO.StreamReader
{
private delegate int ReadMethod(char[] buffer, int index, int count);

string insertStr;
int lineNo, readNo, overflowIndex;
char[] overflow;

ReadMethod[] fnRead;

public InsertLineStreamReader(Stream source, string insertStr, int lineNo) : base( source)
{
this.insertStr = insertStr;
this.lineNo = lineNo;
this.readNo = 0;
this.fnRead = new ReadMethod[] {
new ReadMethod( this.ReadBefore),
new ReadMethod( this.ReadOverflow),
new ReadMethod( this.ReadAfter)
};
this.overflowIndex = 0;
}

//
// . . . Override other methods here (may not be necessary depending on the consumer).
//

public override int Read(char[] buffer, int index, int count)
{
// Use dispatch table of delegates to get the complex override(s)
// out of the way asap.
return this.fnRead[this.readNo](buffer, index, count);
}

public int ReadBefore(char[] buffer, int index, int count)
{
char[] scan = new char[count];
int cchRead = base.Read(scan, 0, count);
if (cchRead > -1)
{
int insOfs, lineCnt = 0;
for (insOfs = 0; insOfs < cchRead && this.lineNo != lineCnt; ++insOfs)
{
if (scan[insOfs] == '\n') ++lineCnt;
}
if (this.lineNo == lineCnt)
{
char[] insertBuf = this.insertStr.ToCharArray( );
if ((insOfs + this.insertStr.Length) > count)
{
int fragmentLen = (insOfs + insertBuf.Length - count);
Array.Copy(scan, 0, buffer, index, insOfs);
Array.Copy(insertBuf, 0, buffer, index + insOfs, fragmentLen);
this.overflow = new char[insertBuf.Length - fragmentLen];
Array.Copy(insertBuf, fragmentLen, this.overflow, 0, this.overflow.Length);
this.readNo = 1;
cchRead = count;
}
else
{
Array.Copy(scan, 0, buffer, index, insOfs);
Array.Copy(insertBuf, 0, buffer, index + insOfs, insertBuf.Length);
int destSpaceRemaining = count - insOfs - insertBuf.Length;
int srcSpaceRemaining = cchRead - insOfs;
if (srcSpaceRemaining > destSpaceRemaining)
{
Array.Copy(scan, insOfs, buffer, index + insOfs + insertBuf.Length, destSpaceRemaining);
this.overflow = new char[srcSpaceRemaining - destSpaceRemaining];
Array.Copy(scan, insOfs + destSpaceRemaining, this.overflow, 0, this.overflow.Length);
this.readNo = 1;
cchRead = count;
}
else
{
Array.Copy(scan, insOfs, buffer, index + insOfs + insertBuf.Length, srcSpaceRemaining);
this.readNo = 2;
cchRead = cchRead + insertBuf.Length;
}
}
}
else
{
Array.Copy(scan, 0, buffer, index, cchRead);
}
}
return cchRead;
}

public int ReadOverflow(char[] buffer, int index, int count)
{
int cchRead;
int overflowLen = this.overflow.Length - this.overflowIndex;

if (count > overflowLen)
{
Array.Copy(this.overflow, this.overflowIndex, buffer, index, overflowLen);
this.overflowIndex = this.overflow.Length;
this.readNo = 2;
cchRead = overflowLen;

int nextCchRead = this.Read( buffer, index + overflowLen, count - overflowLen);
return ( -1 == nextCchRead ) ? cchRead : ( cchRead + nextCchRead );
}

Array.Copy(this.overflow, this.overflowIndex, buffer, index, count);
this.overflowIndex += count;
cchRead = count;
return cchRead;
}

public int ReadAfter(char[] buffer, int index, int count)
{
return base.Read(buffer, index, count);
}
}
}
- - -

Now you might use this to insert an arbitrary line into the beginning of
your XML instance document like this,

FileStream stream = new FileStream("doc.xml", FileMode.Open, FileAccess.Read);
string dtd = "<!-- Ref to your DTD can go here. -->";
InsertLineStreamReader reader = new InsertLineStreamReader(stream, dtd, 1);
XmlValidatingReader xmlReader = new XmlValidatingReader(new XmlTextReader( reader));
// ... wire-up Schema and ValidationEventHandler here.
XmlDocument doc = new XmlDocument( );
doc.Load( xmlReader);

As far as the XmlValidatingReader is concerned, it will receive,

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Ref to your DTD can go here. -->
<XML ... />

even though your file contained,

<?xml version="1.0" encoding="UTF-8" ?>
<XML ... />
Derek Harmon
Nov 12 '05 #2

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

Similar topics

1
by: Rafaela K. Azinhal | last post by:
Please, help me with following code snippet ' Load xml document Dim xmlDoc As New XmlDocument() xmlDoc.Load(http://localhost/VT/reports/VT_actual_owsPivotTable.xml) ' define namespacemanager...
2
by: Beniamin Tecar | last post by:
Hi, I have an xml : <DataRecords> <Point Alias='A' Value='1' Status='0' /> <Point Alias='B' Value='2' Status='0' /> </DataRecords> I have needed by an XSL to insert a node between...
4
by: mike | last post by:
Hello. Let's say I have the following XML document: <ROOT><Element1>Some Text</Element1></ROOT> I want to add a new element after Element1. I tried the following but, naturally, it doesn't...
4
by: Jaime Stuardo | last post by:
Hi all... I need to add some JavaScript code that is dynamically generated to some point of the page. Currently I'm using ClientScript.RegisterStartupScript(GetType(), "menu", "<script...
7
by: E.F | last post by:
Hi everybody, I get lost trying to insert a node in my xml file with readfile, xpath, xpathnavigator, etc... 1/ my xml file looks like that : <PUPILL_CP_1> <YEAR_2006> <OCTOBRE...
1
by: ramjaspari | last post by:
Hi all, I want to add childnodes to a child node(ex:ch1) and adding one more child node to the ch1 node.. and it will keep on going.. Number of nodes is NOT pre-defined.... so...
7
by: sasimca007 | last post by:
Hello friends, What my doubt is, a page is already designed in that page a table exists, and i am doing a script like a radio button exixts and when click that radio button...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.