C# 反序列化不工作的XML包含xmlns
这是我的xml字符串C# 反序列化不工作的XML包含xmlns,c#,xml,serialization,C#,Xml,Serialization,这是我的xml字符串 <test name="james" type="seoul" xmlns="test:client"> <friends xmlns="friends:test"> <friend xmlns="" name="john"> Google </fri
<test name="james" type="seoul" xmlns="test:client">
<friends xmlns="friends:test">
<friend xmlns="" name="john">
Google
</friend>
<friend xmlns="" name="john">
Amazon
</friend>
</friends>
</test>
我永远无法更改xml。我想在friends元素中创建一个内容列表。
请帮帮我 尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(TestModel));
TestModel testModel = (TestModel)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "test", Namespace = "test:client")]
public class TestModel
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Id { get; set; }
[XmlArray(ElementName = "friends", Namespace = "friends:test")]
[XmlArrayItem("friend", Namespace = "")]
public List<Friend> friends { get; set; }
}
public class Friend
{
[XmlText]
public string text { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XmlReader=XmlReader.Create(文件名);
XmlSerializer serializer=新的XmlSerializer(typeof(TestModel));
TestModel TestModel=(TestModel)序列化程序。反序列化(读取器);
}
}
[XmlRoot(ElementName=“test”,Namespace=“test:client”)]
公共类测试模型
{
[XmlAttribute(“名称”)]
公共字符串名称{get;set;}
[XmlAttribute(“类型”)]
公共字符串Id{get;set;}
[XmlArray(ElementName=“friends”,Namespace=“friends:test”)]
[XmlArrayItem(“friend”,Namespace=”“)]
公共列表朋友{get;set;}
}
公课朋友
{
[XmlText]
公共字符串文本{get;set;}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(TestModel));
TestModel testModel = (TestModel)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "test", Namespace = "test:client")]
public class TestModel
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Id { get; set; }
[XmlArray(ElementName = "friends", Namespace = "friends:test")]
[XmlArrayItem("friend", Namespace = "")]
public List<Friend> friends { get; set; }
}
public class Friend
{
[XmlText]
public string text { get; set; }
}
}