C# 如何使用xml序列化映射类对象中的两个列表值
我有两个相同长度的元素列表,例如C# 如何使用xml序列化映射类对象中的两个列表值,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我有两个相同长度的元素列表,例如 List 1: profile1, profile2,profile3 List 2: 1,0,1 这些列表是类对象中的公共属性。 i、 e: class服务信息 { [xmlArray(“Profiles”)] [xmlArrayItem(“配置文件”)] 公开名单简介; 公开名单国家; } 在这里,我需要映射每个配置文件及其状态。例如 <serviceinfo> <profiles> <profile>p
List 1: profile1, profile2,profile3
List 2: 1,0,1
这些列表是类对象中的公共属性。
i、 e:
class服务信息
{
[xmlArray(“Profiles”)]
[xmlArrayItem(“配置文件”)]
公开名单简介;
公开名单国家;
}
在这里,我需要映射每个配置文件及其状态。例如
<serviceinfo>
<profiles>
<profile>profile1</profile>
<state>1</state>
</profiles>
<profiles>
<profile>profile2</profile>
<state>0</state>
</profiles>
<profiles>
<profile>profile3</profile>
<state>1</state>
</profiles>
</serviceinfo>
简介1
1.
简介2
0
简介3
1.
如何更改类对象以返回上述xml结果。是否可以在xml序列化方法中获得上述输出。我不确定是否可以使用
XmlSerializer
完成,因为与类相比,所需的xml结构非常奇怪
但使用LINQ to XML肯定可以做到:
var serviceInfo = new Serviceinfo { Profiles = new List<string> { "one", "two", "three" }, state = new List<int> { 1, 2, 4 } };
var xml = new XDocument(
new XElement("serviceinfo",
serviceInfo.Profiles
.Zip(serviceInfo.state, (p, s) => new { p, s })
.Select(x =>
new XElement("profiles",
new XElement("profile", x.p),
new XElement("state", x.s.ToString())))));
@Gomathipriya:根据我从您的问题中了解到的情况,最好的解决方案是创建一个集合,将两个值都作为以下示例中提到的属性,然后将整个集合转换为XML 将集合转换为XML: 示例C#代码:
类程序
{
公共静态void Main(字符串[]args)
{
列表配置文件=新列表();
列表状态=新列表();
//只需根据您的需要填充实体
对于(int i=0;i<5;i++)
{
Profiles.Add(“P-”+i.ToString());
添加(i);
}
ListOfProflestate ProfileStateColl=新的ListOfProflestate();
对于(int i=0;i
根据上述示例,如果您将集合“ProfileStateColl”转换为XML,您将获得所需的解决方案。:) 不要折磨自己。只需定义一个Profile类并添加两个附加属性Profile和State
var serviceInfo = new Serviceinfo { Profiles = new List<string> { "one", "two", "three" }, state = new List<int> { 1, 2, 4 } };
var xml = new XDocument(
new XElement("serviceinfo",
serviceInfo.Profiles
.Zip(serviceInfo.state, (p, s) => new { p, s })
.Select(x =>
new XElement("profiles",
new XElement("profile", x.p),
new XElement("state", x.s.ToString())))));
[XmlRoot(ElementName = "serviceInfo")]
public class Serviceinfo
{
[XmlElement("profiles")]
public List<Profile> Profiles { get; set; }
}
public class Profile
{
[XmlElement(ElementName = "profile")]
public string Name { get; set; }
[XmlElement(ElementName = "state")]
public int State { get; set; }
}
var serviceInfo = new Serviceinfo
{
Profiles = new List<Profile>() {
new Profile { Name = "one", State = 1 },
new Profile { Name = "two", State = 2 }
}
};
var writer = new StringWriter();
var serializer = new XmlSerializer(typeof(Serviceinfo));
serializer.Serialize(writer, serviceInfo);
var xml = writer.ToString();
class Program
{
public static void Main(string[] args)
{
List<string> Profiles = new List<string>();
List<int> state = new List<int>();
// Just filling the entities as per your needs
for (int i = 0; i < 5; i++)
{
Profiles.Add("P-" + i.ToString());
state.Add(i);
}
listOfProfileState ProfileStateColl = new listOfProfileState();
for (int i = 0; i < Profiles.Count; i++)
{
ProfileStateColl.Add(new ProfileState() { Profile = Profiles[i], State = state[i] });
}
}
}
public class ProfileState
{
public string Profile { get; set; }
public int State { get; set; }
}
public class listOfProfileState : List<ProfileState>
{
}