2012-03-26 27 views
0

我之前沒有喜悅地發佈了這個問題的不同味道。我希望重構的問題會有所幫助。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

我看不出有什麼問題呢。我不需要集合上方的根節點嗎?

+0

爲什麼不直接使用['XmlSerializer.Deserialize(stream)'](http://msdn.microsoft.com/zh-cn/library/dsh84875.aspx)方法?沒有必要做這兩個前面的步驟。 – Groo 2012-03-26 21:16:25

+0

來自一個URI,使用WebClient獲取XML使用'XmlSerializer.Deserialize(stream)'方法得到XML – sambomartin 2012-03-26 21:19:13

+0

同樣的問題,我顯然缺少一些東西,但看不到什麼 – sambomartin 2012-03-26 21:22:46

回答

2

我已經測試的簡化版本(與FeatureCollectionType刪除所有屬性),並沒有在第一個元素拋出:

private static string 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'> 
</osgb:FeatureCollection>"; 

public static void RunSnippet() 
{ 
    using (var sr = new StringReader(xml)) 
    { 
     var xs = new XmlSerializer(typeof(FeatureCollectionType)); 
     var obj = xs.Deserialize(sr); 
     Console.WriteLine("if you are reading this, it didn't throw"); 
    } 
} 

你怎麼實例XmlSerializer

+0

謝謝你們!爲理智檢查groo而歡呼。在此期間,他正在做類似的事情,剝離它。事實證明,我在同一個命名空間中有另一個版本的xsd生成的類。因爲xsd.exe會生成部分類,它不會被ide拋出。希望這可以幫助別人犯一個愚蠢的錯誤。感謝你的時間(並且很抱歉浪費它!) – sambomartin 2012-03-26 21:54:47

相關問題