使用 linq 在运行时动态构建 xml 树

发布于 2024-10-01 01:09:27 字数 626 浏览 1 评论 0原文

这是一个设计问题,吊起大师的胃口。

在以前的版本中,代码使用路径定义,即 /../../ 来提供有关如何构建 artbitary xml 文档的说明。根据 xml 模式,文档必须是正确的,因此路径本质上是 xpath,在 switch 语句中读取和使用以调用代码来构造文档。但不要沉迷于此。

例如,读入和编码时的调用

jim.set("/Alert/Source/DetectTime", "12:03:2010 12:22:21");

将创建

<Alert> 
  <Source>
    <DetectTime>12:03:2010 12:22:21</DetectTime>
  </Source>
</Alert>

jim.set 只是将树存储在特定的上下文中。

现在我想使用 linq to xsd 来创建 xml 树,但我仍在寻找一些结构,使用户能够定义如何构建树。 XPath 很简单,但实现起来有点麻烦,不优雅,而且我不方便映射。

任何想法、任何程序、开源、研究,任何东西都会受到赞赏。如果我没有得到答复,我将在 3 天内提供可观的赏金。 鲍勃.

This is a design question, to whet the guru's appetite.

In a previous version, the code was using a path definition, i.e. /../../ to provide instructions on how to build artbitary xml documents. The documents had to be correct according to xml schema, so the path's were essentially xpaths, being read and used in switch statements to call code to construct the doc. But don't get hung up on it.

For example the call when read in and coded

jim.set("/Alert/Source/DetectTime", "12:03:2010 12:22:21");

would create

<Alert> 
  <Source>
    <DetectTime>12:03:2010 12:22:21</DetectTime>
  </Source>
</Alert>

The jim.set just stores the tree in a particular context.

Now I'd like to use linq to xsd, to create the xml tree, but i'm still looking for some contruct that will enable a user to define how the tree will be constructed. XPath's are easy, but kinda bulky to implement, not elegant and I don't easy mapping.

Any ideas, any programs, open source, research, anything would be apreciated. If I don't get an answer I'll offer a decent bounty in 3 days.
Bob.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2024-10-08 01:09:27

做这样的事情怎么样:

public XDocument CreateXDocument(string path, string value)
{
    var parts = path.Split(new [] { '/', },
        StringSplitOptions.RemoveEmptyEntries);
    return new XDocument(this.CreateXElement(parts, value));
}

private XElement CreateXElement(IEnumerable<string> parts, string value)
{
    var content = parts.Count() == 1 ?
        (object)value :
        (object)this.CreateXElement(parts.Skip(1), value);
    return new XElement(parts.First(), content);
}

然后您可以运行这样的代码:

var xd = this.CreateXDocument("/Alert/Source/DetectTime", "12:03:2010 12:22:21");
Console.WriteLine(xd.ToString());

它将产生:

<Alert>
  <Source>
    <DetectTime>12:03:2010 12:22:21</DetectTime>
  </Source>
</Alert>

我想我可能会发布 CreateXElement 方法的变体,它应该更具可读性。功能是相同的,甚至可能会有边际性能改进。

private XElement CreateXElement(IEnumerable<string> parts, string value)
{
    var head = parts.First();
    var tail = parts.Skip(1);
    var content = tail.Any() ?
        (object)this.CreateXElement(tail, value) :
        (object)value;
    return new XElement(head, content);
}

How about doing something like this:

public XDocument CreateXDocument(string path, string value)
{
    var parts = path.Split(new [] { '/', },
        StringSplitOptions.RemoveEmptyEntries);
    return new XDocument(this.CreateXElement(parts, value));
}

private XElement CreateXElement(IEnumerable<string> parts, string value)
{
    var content = parts.Count() == 1 ?
        (object)value :
        (object)this.CreateXElement(parts.Skip(1), value);
    return new XElement(parts.First(), content);
}

You can then run the code like this:

var xd = this.CreateXDocument("/Alert/Source/DetectTime", "12:03:2010 12:22:21");
Console.WriteLine(xd.ToString());

Which will produce:

<Alert>
  <Source>
    <DetectTime>12:03:2010 12:22:21</DetectTime>
  </Source>
</Alert>

I thought I might post a variation of the CreateXElement method that should be more readable. The functionality is the same and there may even be a marginal performance improvement.

private XElement CreateXElement(IEnumerable<string> parts, string value)
{
    var head = parts.First();
    var tail = parts.Skip(1);
    var content = tail.Any() ?
        (object)this.CreateXElement(tail, value) :
        (object)value;
    return new XElement(head, content);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文