目次
XMLファイル
サンプル5 XmlDocumentの再帰追跡
型指定の無いデータセットをWebサービスのレスポンスで返すサンプルをXmlDocumentに読み込みます。
//型指定されていないデータセットの読み込み
XmlDocument doc = new XmlDocument();
XmlTextReader tr = new XmlTextReader("./DataSetRequest.xml");
XmlValidatingReader reader = new XmlValidatingReader(tr);
reader.ValidationType = ValidationType.Auto ;
/// reader.ValidationType = ValidationType.DTD ;
/// reader.ValidationType = ValidationType.Schema ;
/// reader.ValidationType = ValidationType.XDR ;
/// reader.ValidationType = ValidationType.None ;
doc.Load(reader);
このまま実行するとエラーが発生します。Message "'urn:schemas-microsoft-com:xml-diffgram-v1:diffgram' 要素が宣言されていません。
DTD・XDR・Noneの /// をはずすと読み込めます。
こんなコードで追跡できます。
foreach ( XmlNode node in doc )
{
DumpNext(node) ;
}
private void DumpNext(XmlNode node)
{
System.Diagnostics.Debug.Write(node.Name+"->"+node.Value+"->") ;
if ( node.Attributes != null )
{
foreach ( XmlAttribute attr in node.Attributes )
{
System.Diagnostics.Debug.Write(attr.Name+"="+attr.Value+" ") ;
}
}
System.Diagnostics.Debug.WriteLine("") ;
if ( node.HasChildNodes )
{
foreach ( XmlNode node0 in node )
{
GetNext(node0) ;
}
}
}
xml->version="1.0" encoding="utf-8"->
DataSet->->xmlns=http://localhost/DatasetService1
xs:schema->->id=DataSet1 targetNamespace=http://www.tempuri.org/DataSet1.xsd xmlns:mstns=http://www.tempuri.org/DataSet1.xsd xmlns=http://www.tempuri.org/DataSet1.xsd xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns:msdata=urn:schemas-microsoft-com:xml-msdata attributeFormDefault=qualified elementFormDefault=qualified
xs:element->->name=DataSet1 msdata:IsDataSet=true msdata:Locale=ja-JP
xs:complexType->->
xs:choice->->maxOccurs=unbounded
xs:element->->name=Customers
xs:complexType->->
xs:sequence->->
xs:element->->name=CustomerID type=xs:string
xs:element->->name=CompanyName type=xs:string
xs:unique->->name=Constraint1 msdata:PrimaryKey=true
xs:selector->->xpath=.//mstns:Customers
xs:field->->xpath=mstns:CustomerID
diffgr:diffgram->->xmlns:msdata=urn:schemas-microsoft-com:xml-msdata xmlns:diffgr=urn:schemas-microsoft-com:xml-diffgram-v1
DataSet1->->xmlns=http://www.tempuri.org/DataSet1.xsd
Customers->->diffgr:id=Customers1 msdata:rowOrder=0
CustomerID->->
#text->ALFKI->
CompanyName->->
#text->Alfreds Futterkiste->
Customers->->diffgr:id=Customers2 msdata:rowOrder=1
CustomerID->->
#text->ANATR->
CompanyName->->
#text->Ana Trujillo Emparedados y helados->
Customers->->diffgr:id=Customers3 msdata:rowOrder=2
CustomerID->->
#text->ANTON->
CompanyName->->
#text->Antonio Moreno Taqueria->
Customers->->diffgr:id=Customers4 msdata:rowOrder=3
CustomerID->->
#text->AROUT->
CompanyName->->
#text->Around the Horn->



