XML 反序列化系统.无效操作异常" <对象 xmlns=> 不是预期的



因为两天我无法反序列化XML文件。我尝试了很多不同的想法,但仍然没有成功。我在1940年1月1日反序列化名为phone.xml的xml文件时遇到一个错误。我认为可能是[XmlElement("id"(]括号中缺少命名空间的问题,但没有更改。我复制了不同的反序列化思想。没有成功。此外,我甚至不能用任何转换器转换这个xml,但我确信它是正确的。帮助

链接内部xml结构的jpeg示例

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

public class OrderedItem
{
[XmlElement("id")]
public string id;
[XmlElement("username")]
public string username;
[XmlElement("phoneType")]
public string phoneType;
[XmlElement("value")]
public string value;
}
public class Program
{
public static void Main()
{
Program t = new Program();
t.DeserializeObject("phone.xml");
}
private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with TextReader");
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
TextReader reader = new StreamReader(fs);
OrderedItem i;

i = (OrderedItem)serializer.Deserialize(reader);
Console.Write(
i.id + "t" +
i.username + "t" +
i.phoneType + "t" +
i.value + "t");
}
}

XML示例

<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object>
<Property Name="id"></Property>
<Property Name="username">JLOCK0</Property>
<Property Name="phoneType">phone1</Property>
<Property Name="value">346-209-9609</Property>
</Object>
<Object>
<Property Name="id"></Property>
<Property Name="username">JLOCK0</Property>
<Property Name="phoneType">phone2</Property>
<Property Name="value">(478) 8085400</Property>
</Object>
</Objects>
public class Objects
{
[XmlElement("Object")]
public OrderedItem[] Items;
}
public class OrderedItem
{
[XmlElement("Property")]
public Property[] Properties;
}
public class Property
{
[XmlAttribute]
public string Name { get; set; }
[XmlText]
public string Value { get; set; }
}
var serializer = new XmlSerializer(typeof(Objects));
using (var fs = new FileStream(filename, FileMode.Open))
{
var objects = (Objects)serializer.Deserialize(fs);
foreach (var item in objects.Items)
{
foreach (var prop in item.Properties)
{
Console.WriteLine(prop.Name + " " + prop.Value);
}
Console.WriteLine();
}
}

相关内容

  • 没有找到相关文章

最新更新