"terry" <te***@discussions.microsoft.com> wrote in message news:DA**********************************@microsof t.com...
could someone tell me how to add or remove entity to a xml file
XmlEntity can't be directly instantiated, you have to work through the InternalSubset
of the DTD (as represented by the XmlDocumentType class). When you specify
the InternalSubset for an XmlDocumentType object that you can create through
an XmlDocument's CreateDocumentType( ) method, .NET will parse the Entities
and build a collection of XmlEntity objects for you.
Given the following XML document,
- - - book.xml
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
<book genre='technology' ISBN='0-764548-26-3'>
<title>Visual Basic .NET Bible</title>
<style>&h;</style>
</book>
- - -
You might load it with the following VB.NET code,
Dim xmlDoc As New XmlDocument( )
xmlDoc.Load( "book.xml")
Then you'd create a pair of XmlDocumentType objects,
Dim oldDTD As XmlDocumentType = xmlDoc.DocumentType
Dim newDTD As XmlDocumentType
newDTD = xmlDoc.CreateDocumentType( _
oldDTD.Name, _
oldDTD.PublicId,_
oldDTD.SystemId, _
"<!ENTITY h 'softcover'>" _
)
Notice that I've used the CreateDocumentType( ) method of the XmlDocument
to create the new DTD. I copy it's primary characteristics (name, public and
system identifiers) from whatever the old DTD contained, and then I supply the
text of the internal subset I wish the DTD to use (it's left as an exercise for the
reader to enumerate Entities and Notations of the existing DTD to be maintained;
Hint: you can start by using the XmlDocumentType's InternalSubset string property
as the basis for keeping/revising tracts of text). In the replacement entity here,
I've redefined &h; to refer to the literal text, "softcover," instead of "hardcover."
Now, an XmlDocument can only have one XmlDocumentType. So to apply this
XmlDocumentType, and it's newly created Entities, I must replace the existing
XmlDocumentType node like so,
xmlDoc.ReplaceChild( newDTD, oldDTD)
If I now examine the contents of the DTD on the XmlDocument, I can see the
changes have taken effect.
Console.WriteLine( xmlDoc.DocumentType.OuterXml)
Finally, the XmlEntity that you wanted to create (to recap, it was the following
text I passed to the internalSubset argument of CreateDocumentType( )):
<!ENTITY h 'softcover'>
has been created for you in the Entitites collection of xmlDoc.DocumentType.
It can be examined like this:
Dim entity As XmlEntity = CType( xmlDoc.DocumentType.Entities, GetType( XmlEntity))
Console.Write( "{0} ", entity.NodeType)
Console.Write( "{0} ", entity.Name)
Console.Write( "{0} ", entity.NotationName)
Console.Write( "{0} ", entity.PublicId)
Console.Write( "{0} ", entity.SystemId)
Console.Write( "{0} ", entity.InnerText)
Derek Harmon