2012-10-19 60 views
1

我遇到問題。我寫了一個程序,將大量的數據轉換爲VML-GML格式。問題是我需要使用XmlWriter ...現在我有一個問題,我下面的方法:XmlWriter使命名空間不正確

private void StartDocument() 
{ 
    _writer.WriteStartDocument(); 
    _writer.WriteStartElement("osgb", "FeatureCollection", "osgb"); 
    _writer.WriteAttributeString("osgb", "xmlns", "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1"); 
    _writer.WriteAttributeString("gml", "xmlns", "http://www.opengis.net/gml"); 
    _writer.WriteAttributeString("xsi", "xmlns", "http://www.w3.org/2001/XMLSchema-instance"); 
    _writer.WriteAttributeString("schemaLocation", "xsi", 
           "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd"); 
    _writer.WriteAttributeString("fid", ""); // TODO: set fid here 

    _writer.WriteStartElement("gml", "description", "gml"); 
    _writer.WriteValue("Ordnance Survey, (c) Crown Copyright. All rights reserved, 2011-03-02"); 
    _writer.WriteEndElement(); // description 

    _writer.WriteElementString("osgb", "creationDate", "osgb", DateTime.Today.ToString("yyyy-MM-dd")); 
} 

如何在正確的命名空間寫?我這樣做那樣,輸出是:

<?xml version="1.0" encoding="utf-8"?> 
<osgb:FeatureCollection p1:osgb="http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1" 
p1:gml="http://www.opengis.net/gml" 
p1:xsi="http://www.w3.org/2001/XMLSchema-instance" 
p2:schemaLocation="http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd" 
fid="" xmlns:p2="xsi" xmlns:p1="xmlns" xmlns:osgb="osgb"> 

而這正是我需要的:

<osgb:FeatureCollection xmlns:osgb="http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1" 
xmlns:gml="http://www.opengis.net/gml" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd" 
fid=""> 

爲什麼會這樣愚蠢XmlWriter創造一個像p1p2等這樣的事情?

Btw。我試圖在使用這些VML-GML文件的程序中打開我的輸出文件,並告訴我該文件格式不正確。當我手動將名稱空間更改爲正確的名稱空間時,一切都很好。

如何糾正它?提前致謝!

+0

的CuzüR值不要使用linq2Xml – Anirudha

+0

我,因爲大量的數據不是。 – Nickon

+0

linq2xml可以處理..沒有問題在它..它是一個完全替代其他XML API .. – Anirudha

回答

8

爲什麼這個愚蠢的XmlWriter創建像p1,p2等這樣的東西?

你有你的屬性值以錯誤的方式,您還需要指定另一種說法:

此:

_writer.WriteAttributeString("gml", "xmlns", "http://www.opengis.net/gml"); 

應該是這樣的:

_writer.WriteAttributeString("xmlns", "gml", null, "http://www.opengis.net/gml"); 

此外,您的元素編寫代碼不正確。你應該指定名稱空間URI時指定前綴。

我相信這並不需要什麼:

_writer.WriteStartDocument(); 
// The first argument here isn't really needed, but is changes whether a 
// prefix is used or not 
_writer.WriteStartElement("osgb", "FeatureCollection", "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1"); 
_writer.WriteAttributeString("xmlns", "osgb", null, "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1"); 
_writer.WriteAttributeString("xmlns", "gml", null, "http://www.opengis.net/gml"); 
_writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); 
_writer.WriteAttributeString("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", 
          "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd"); 
_writer.WriteAttributeString("fid", ""); // TODO: set fid here 

_writer.WriteStartElement("description", "http://www.opengis.net/gml"); 
_writer.WriteValue("Ordnance Survey, (c) Crown Copyright. All rights reserved, 2011-03-02"); 
_writer.WriteEndElement(); // description 

_writer.WriteElementString("creationDate", "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1", DateTime.Today.ToString("yyyy-MM-dd")); 
_writer.WriteEndElement(); 

(我個人使用LINQ到XML,這使得這一切簡單的,但嘿...)

作爲提示,總是假設你的代碼是錯誤的,而不是框架代碼,作爲一個起點。將框架類視爲「愚蠢」可能會導致您沒有足夠小心地查看自己的代碼。

編輯:這裏是工作的LINQ to XML代碼:

using System; 
using System.Xml.Linq; 

static class Test 
{ 
    static void Main() 
    { 
     XNamespace osgb = "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1"; 
     XNamespace gml = "http://www.opengis.net/gml"; 
     XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 

     var doc = new XDocument(
      new XElement(osgb + "FeatureCollection", 
       new XAttribute(XNamespace.Xmlns + "osgb", osgb), 
       new XAttribute(XNamespace.Xmlns + "gml", gml), 
       new XAttribute(XNamespace.Xmlns + "xsi", xsi), 
       new XAttribute(xsi + "schemaLocation", "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd"), 
       new XAttribute("fid", ""), 
       new XElement(gml + "description", "Ordnance Survey ..."), 
       new XElement(osgb + "creationDate", 
          // TODO: Find a better way of doing this 
          DateTime.Today.ToString("yyyy-MM-dd")))); 
     Console.WriteLine(doc); 
    } 
} 

輸出:

<osgb:FeatureCollection 
    xmlns:osgb="http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1" 
    xmlns:gml="http://www.opengis.net/gml" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd" 
    fid=""> 
    <gml:description>Ordnance Survey ...</gml:description> 
    <osgb:creationDate>2012-10-19</osgb:creationDate> 
</osgb:FeatureCollection> 
+0

我得到錯誤'前綴「xmlns」被保留供XML使用。順便說一句,爲什麼如果方法採用'WriteAttributeString(string localName,string ns,string value)'這個不好? – Nickon

+1

@Nickon:現在修復 - 基本上我讀了XmlWriter.WriteStartAttribute的文檔... –

+0

大數據量呢?如何處理? – Nickon

相關問題