XPSドキュメントからテキストを取得し、必要に応じて使用することができました(この回答のおかげです)が、XmlReader
すべての要素を自動的にコードなどでループできるオブジェクト コレクション。
これは不自然な例ですが、次の疑似コードのようなものです。
'open the xps document
Dim xpsDoc As New XpsDocument(pathToTestXps, System.IO.FileAccess.Read)
'load the fixed document squences
Dim fixedDocSeqReader As IXpsFixedDocumentSequenceReader = xpsDoc.FixedDocumentSequenceReader
'the content will go here
Dim sbContent As New System.Text.StringBuilder()
'loops the fixed focuments
For Each docReader As IXpsFixedDocumentReader In fixedDocSeqReader.FixedDocuments
'loop the fixed pages
For Each fixedPageReader As IXpsFixedPageReader In docReader.FixedPages
'BEGIN PSEUDO CODE
Dim content as IXpsContentCollection = fixedPageReader.Contents
For Each contentItem as IXpsContentItem In Contents
Select Case contentItem.Type
Case IXpsContentItem.ContentType.Canvas 'Group
'loop content items, check their type, do stuff
Case IXpsContentItem.ContentType.Glyph 'Text
Dim str As String = DirectCast(contentItem, Glyph).UniCodeString
'do something with the string
Case IXpsContentItem.ContentType.Path 'Shape
'get the shape properties etc
Case Else
Throw New ApplicationException("XPS Content Type Not Expected:" & contentItem.Type.ToString)
End Select
Next
'END PSEUDO CODE
Next
Next
そのようなモデルがない場合、XMLReader を使用する最も簡単な方法は何ですか?XML 要素と属性の適切なリファレンスはありますか?
コンテキストのために、現時点では、上記の擬似コードの代わりにこれを行っています。
'get the xml for the fixed pages
Dim pageContentReader As System.Xml.XmlReader = fixedPageReader.XmlReader
While pageContentReader.Read()
'if it is a canvas, it's a new line or some other stuff
If pageContentReader.Name = XmlElementCanvas Then
'other stuff won't have attibutes
If pageContentReader.HasAttributes Then
'remove the last char as it will be an excess comma
If sbContent.Length > 0 Then
sbContent.Length = sbContent.Length - 1
sbContent.AppendLine()
End If
End If
End If
'if it is a glyph, it's the text we want
If pageContentReader.Name = XmlElementGlyphs Then
'unsure, but it was in the example code, so we'll keep it
If pageContentReader.HasAttributes Then
'unicode string attribute has the text we want
If pageContentReader.GetAttribute(XmlAttribUnicodeString) IsNot Nothing Then
'add the text and a comma
sbContent.Append(pageContentReader.GetAttribute(XmlAttribUnicodeString))
sbContent.Append(",")
End If
End If
End If
End While