如何创建扩展方法以将我的List of T转换为XML字符串。 我的T对象的属性变为xml标记,属性的值成为xml标记中的值。 My T对象具有简单的字符串属性,即没有集合或二维对象。这就是所有属性都是字符串,int等,即一维......没有列出/数组作为属性。
答案 0 :(得分:2)
如果你想转换例如这种列表:
List<int> Branches = new List<int>();
Branches.Add(1);
Branches.Add(2);
Branches.Add(3);
进入这个XML:
<Branches>
<branch id="1" />
<branch id="2" />
<branch id="3" />
</Branches>
您可以使用LINQ尝试此操作:
List<int> Branches = new List<int>();
Branches.Add(1);
Branches.Add(2);
Branches.Add(3);
XElement xmlElements = new XElement("Branches", Branches.Select(i => new XElement("branch", new XAttribute("id", i))));
System.Console.Write(xmlElements);
System.Console.Read();
输出:
<Branches>
<branch id="1" />
<branch id="2" />
<branch id="3" />
</Branches>
您需要包含using System.Xml.Linq;
命名空间。
编辑:要制作文件,您可以使用此方法
public string ToXML<T>(T obj)
{
using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(stringWriter, obj);
return stringWriter.ToString();
}
}
答案 1 :(得分:2)
您所谈论的内容大致转换为“序列化”,并且像大多数通用问题一样,这个问题已经解决了。该框架当然为您提供了一些Xml序列化工具。
给出一个课程:
public class TestClass
{
public string Prop1 {get;set;}
public string Prop2 {get;set; }
}
和扩展方法:
public static class SerializationExtensions
{
public static string ToXml<T>(this List<T> list)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<T>));
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlSerializer.Serialize(xmlWriter, list);
return stringWriter.ToString();
}
}
一个简单的演示生成xml
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TestClass>
<Prop1>val1</Prop1>
<Prop2>val2</Prop2>
</TestClass>
<TestClass>
<Prop1>val1</Prop1>
<Prop2>val2</Prop2>
</TestClass>
<TestClass>
<Prop1>val1</Prop1>
<Prop2>val2</Prop2>
</TestClass>
</ArrayOfTestClass>
序列化到文件而不是字符串是微不足道的,但为了演示用法,它更容易输出为字符串。
答案 2 :(得分:0)
创建扩展方法与常规方法没有太大区别。如果使用关键字“this”指定第一个参数(方法将扩展的对象),则只需将方法设为静态。其余的只是计划反思。
public static string GetXML<T>(this List<T> src)
{
// First, we have to determine the "Type" of the generic parameter.
Type srcType = src.GetType();
Type[] srcTypeGenArgs = srcType.GetGenericArguments();
if (srcTypeGenArgs.Length != 1)
throw new Exception("Only designed to work on classes with a single generic param.");
Type genType = srcTypeGenArgs[0];
// Get the properties for the generic Type "T".
System.Reflection.PropertyInfo[] typeProps = genType.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.GetProperty);
// Now, we loop through each item in the list and extract the property values.
StringBuilder sb = new StringBuilder();
sb.AppendLine("<root>");
for (int i = 0; i < src.Count; i++)
{
T listItem = src[i];
for (int t = 0; t < typeProps.Length; t++)
{
object propVal = typeProps[t].GetValue(listItem, null); // Always pass "null" for the index param, if we're not dealing with some indexed type (like an array).
string propValStr = (propVal != null ? propVal.ToString() : string.Empty);
sb.AppendLine(string.Format("<{0}>{1}</{0}>", typeProps[t].Name, propValStr));
}
}
sb.AppendLine("</root>");
return sb.ToString();
}