0

xmlファイルを逆シリアル化しようとしています。私は過去にこれをやってきましたが、それはいつも魅力のように働いていましたが、今回はそうではありませんでした。誰かが私の間違いを見て助けてくれれば、それは非常に感謝しています!最後に私はそれを1つにして作業していましたが、今回はビジュアルスタジオコンソールアプリケーションです。ビジュアルスタジオで通常の方法でXMLファイルの非直列化が機能しない

編集: Visual Studioで作業するときは、何とか "xmlns"属性を設定する必要がありますが、何にするのでしょうか?

私は、このエラーメッセージが表示されます:

Information.xml:

<?xml version="1.0" encoding="utf-8"?> 
<InformationenContainer> 
    <Informations> 
    <Information ID="foo"/> 
    </Informations> 
</InformationenContainer> 

のProgram.cs:

class Program 
{ 
    static void Main(string[] args) 
    { 
     string filename = "C:\\Users\\Ruben Bohnet\\Documents\\Visual Studio 2015\\Projects\\Tool\\Tool\\Informationen.xml"; 
     InformationenContainer IC = InformationenContainer.Load(filename); 
    } 
} 

Error in XML-Document (2,2) 
<Information xmlns=''> not exspected 

は、私のファイルは、次のようになり

InformationController.cs:

using System.IO; 
using System.Xml.Serialization; 


[XmlRoot("InformationenContainer")] 
public class InformationenContainer 
{ 
    [XmlArray("Informations"), XmlArrayItem("Information",typeof(Information))] 
    public Information[] Informations; 

    public static InformationenContainer Load(string path) 
    { 
     var serializer = new XmlSerializer(typeof(InformationenContainer)); 
     using (var stream = new FileStream(path, FileMode.Open)) 
     { 
      InformationenContainer ic = serializer.Deserialize(stream) as InformationenContainer; 
      return ic; 
     } 
    } 

} 

Information.cs:

using System; 
using System.Xml.Serialization; 

public class Information 
{ 
    //Attribuites to serialize 
    [XmlAttribute("ID")] 
    public String ID; 
} 
+1

Th e読み込みしようとしているファイルへのパスが、あなたが言及したファイルの名前と一致しない - それは単にタイプミスかバグでしょうか? – jropella

+0

ねえ、ちょうどタイプミスでした。しかし、見ていただきありがとうございます!オブジェクトを作成してそれをシリアライズするだけで解決策を見つけました。基本的にそれを逆にしてください:) –

答えて

0

がちょうどInformationContainerを作成し、それをシリアル化することによって溶液自分自身を発見、結果のXMLファイルには、このように見えました:

<?xml version="1.0"?> 
<InformationenContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Informations> 
    <Information ID="foo" /> 
    </Informations> 
</InformationenContainer> 
+1

また、これを行うこともできます:http://stackoverflow.com/questions/870293/can-i-make-xmlserializer-ignore-the-namespace-on-deserialization – mageos

関連する問題