Linq 查询从对象数组中获取数据 C#
public struct Parameter
{
public Parameter(string name, string type, string parenttype)
{
this.Name = name;
this.Type = type;
this.ParentType = parenttype;
}
public string Name;
public string Type;
public string ParentType;
}
以下值存储在参数数组中:
Name Type ParentType
-------------------------------------------------------
composite CompositeType
isThisTest boolean
BoolValue boolean CompositeType
StringValue string CompositeType
AnotherType AnotherCompositeType CompositeType
account string AnotherCompositeType
startdate date AnotherCompositeType
我想读取此值来构建 xml。类似:
<composite>
<BoolValue>boolean</BoolValue>
<StringValue>string</StringValue>
<AnotherType>
<account>string</account>
<startdate>date</startdate>
</AnotherType>
<composite>
<isThisTest>boolean</isThisTest>
我使用以下逻辑来读取值:
foreach (Parameter parameter in parameters)
{
sb.Append(" <" + parameter.Name + ">");
//HERE: need to check parenttype and get all it's child elements
//
sb.Append("</" + parameter.Name + ">" + CrLf);
}
是否有更简单的方法来读取数组以获取父母和他们的孩子?可能正在使用 LINQ?我仍然使用.Net 3.5。感谢任何有关示例代码的建议:)
public struct Parameter
{
public Parameter(string name, string type, string parenttype)
{
this.Name = name;
this.Type = type;
this.ParentType = parenttype;
}
public string Name;
public string Type;
public string ParentType;
}
Following values are stored in the array of Parameter:
Name Type ParentType
-------------------------------------------------------
composite CompositeType
isThisTest boolean
BoolValue boolean CompositeType
StringValue string CompositeType
AnotherType AnotherCompositeType CompositeType
account string AnotherCompositeType
startdate date AnotherCompositeType
I want to read this to build an xml. something like:
<composite>
<BoolValue>boolean</BoolValue>
<StringValue>string</StringValue>
<AnotherType>
<account>string</account>
<startdate>date</startdate>
</AnotherType>
<composite>
<isThisTest>boolean</isThisTest>
I am using the following logic to read the values:
foreach (Parameter parameter in parameters)
{
sb.Append(" <" + parameter.Name + ">");
//HERE: need to check parenttype and get all it's child elements
//
sb.Append("</" + parameter.Name + ">" + CrLf);
}
Is there a simpler way to read the array to get the parents and thier child? May be using LINQ? I still on .Net 3.5. Appreciate any suggestions with example code :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写一些递归方法来处理此问题:
每次调用都会构建一个 的 Enumerable XElements 包含其父级具有传入类型的参数。 select 再次递归到该方法中,查找每个元素的子元素。
请注意,这确实假设数据格式正确。如果两个参数彼此作为父参数,您将得到堆栈溢出。
神奇之处在于 XElements 类 (Linq to Xml),它接受 XElements 的可枚举值来构建类似 Xml 结构的树。
第一次调用,传递 null(如果使用 C# 4,则使用默认参数)作为 rootType。使用如下:
输出:
编辑
为了响应您的评论,XElement 为您提供了两个作为字符串输出的选项。
ToString() 将输出格式化的 Xml。
ToString(SaveOptions) 允许您指定格式化或未格式化的输出以及省略重复的命名空间。
我确信如果确实需要的话,您可能可以调整解决方案以使用 StringBuilder,尽管它可能不会那么优雅。
You could write a little recursive method to deal with this :
Each call builds up an Enumerable of XElements which contains the parameters whose parent has the passed in type. The select recurses into the method again finding the children for each Element.
Note that this does assume that the data is correctly formed. If two parameters has eachother as a parent you will get a Stack Overflow.
The magic is in the XElements class (Linq to Xml) that accepts enumerables of XElements to build up the tree like Xml structure.
The first call, pass null (or use default parameters if using C# 4) as the rootType. Use like :
Output :
Edit
In response to your comment, XElement gives you two options for outputting as a string.
ToString() will output formatted Xml.
ToString(SaveOptions) allows you to specify formatted or unformatted output as well as ommitting duplicate namespaces.
I'm sure you could probably adapt the solution to use StringBuilder if you really had to, although it probably wouldn't be as elegant..
看起来您想使用递归方法,例如:
查看 Parameter 是否具有子节点的方法如下所示:
集合
parameters
可以定义为IEnumerable
IEnumerable
。 Parameter>
并且可以使用List
来实现。It looks like you want to use a recursive method, something like:
The method that looks to see whether a Parameter has child nodes would look like:
The collection
parameters
could be defined as anIEnumerable<Parameter>
and could be implemented using aList<Parameter>
.