我之前沒有喜悅地發佈了這個問題的不同味道。我希望重構的問題會有所幫助。C#XmlSerializer - 在XML文檔0,0中有錯誤)<nodename>不是預期的
本質上,我將XML字符串加載到XDocument中,然後使用XDocument.GetReader()返回一個XmlReader。從這裏我使用的XmlSerializer到deserialise以下XML:
<?xml version='1.0' encoding='UTF-8'?>
<osgb:FeatureCollection
xmlns:osgb='http://www.ordnancesurvey.co.uk/xml/namespaces/osgb'
xmlns:gml='http://www.opengis.net/gml'
xmlns:xlink='http://www.w3.org/1999/xlink'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://www.ordnancesurvey.co.uk/xml/namespaces/osgb http://www.ordnancesurvey.co.uk/xml/schema/v7/OSDNFFeatures.xsd'
fid='GDS-58116-1'>
<gml:description>OS</gml:description>
<gml:boundedBy>
<gml:null>unknown</gml:null>
</gml:boundedBy>
<osgb:queryTime>2009-07-30T02:35:17</osgb:queryTime>
<osgb:queryExtent>
<osgb:Rectangle srsName='osgb:BNG'>
<gml:coordinates>291000.000,92000.000 293000.000,94000.000</gml:coordinates>
</osgb:Rectangle>
</osgb:queryExtent>
</osgb:FeatureCollection>
這個類(使用生成XSD)
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.ordnancesurvey.co.uk/xml/namespaces/osgb")]
[System.Xml.Serialization.XmlRootAttribute("FeatureCollection", Namespace = "http://www.ordnancesurvey.co.uk/xml/namespaces/osgb", IsNullable = false)]
public partial class FeatureCollectionType : AbstractFeatureCollectionType
{
private System.DateTime queryTimeField;
private GeometryPropertyType queryExtentField;
private System.DateTime queryChangeSinceDateField;
private bool queryChangeSinceDateFieldSpecified;
private FeatureAssociationType[] _featureMemberField;
private BoundingShapeType boundedBy1Field;
// more properties
}
內部異常表明:<FeatureCollection xmlns='http://www.ordnancesurvey.co.uk/xml/namespaces/osgb'> was not expected
。
我看不出有什麼問題呢。我不需要集合上方的根節點嗎?
爲什麼不直接使用['XmlSerializer.Deserialize(stream)'](http://msdn.microsoft.com/zh-cn/library/dsh84875.aspx)方法?沒有必要做這兩個前面的步驟。 – Groo 2012-03-26 21:16:25
來自一個URI,使用WebClient獲取XML使用'XmlSerializer.Deserialize(stream)'方法得到XML – sambomartin 2012-03-26 21:19:13
同樣的問題,我顯然缺少一些東西,但看不到什麼 – sambomartin 2012-03-26 21:22:46