【问题标题】:Turn a System.Collections.Generic.Dictionary into an XDocument将 System.Collections.Generic.Dictionary 转换为 XDocument
【发布时间】:2013-05-08 08:31:12
【问题描述】:

我正在尝试将字典制作成 XDocument (XML),以便稍后在我的应用程序中进行解析。

应用程序将基于 XDocument 生成一个 Word 文档。我的朋友会给我寄一本字典,所以我必须自己转换它。

这是我到目前为止所得到的,但我被卡住了。我实在想不出如何继续:

static XDocument Parse(Dictionary<String, String> dic)
    {
        XDocument newXDoc = new XDocument();
        List<String> paragraphs = new List<String>();
        int count = 0;
        foreach(KeyValuePair<string,string> pair in dic)
        {
            if (pair.Key.Equals("paragraph" + count))
            {
                paragraphs.Add(pair.Value);
                count++;
            }
        }
        newXDoc.Add(new XElement("document",
            new XElement("title",dic["title"])));
        return newXDoc;
    }

所以解释一下:

我的想法是把 XML 文档做成这样:

<document>
    <title>Some Title</title>
    <paragraph0>some text</paragraph0>
    <paragraph1>some more text on a new line</paragraph1>
    <paragraph2>
        <list>
            <point>Some Point</point>
            <point>Some other point</point>
        </list>
    </paragraph2>
    <header>
        <author>me</author>
        <date>today</date>
        <subject>some subject</subject>
    </header>
</document>

我的问题是我永远不知道我会收到多少段落,因为我收到的只是一本字典。正如您可能知道的那样,我正在思考如何制作一个不错的foreachconstruction,它可以:

  1. 暂时取出所有段落
  2. 用适当的 XElement 填写 XDocument

我只是不知道如何在不遇到可能的 NullPointerExceptions 或类似情况的情况下执行此操作。有什么帮助吗?

简短版:在给定上述结构的情况下,我如何将字典解析为 XDocument,但不知道我可以获得多少段?

新段落定义为前一段到达换行符 (\n)

【问题讨论】:

  • 你为什么要给段落编号?如果你不这样做,你的问题会消失吗?那么你可能会使用 Linq2Xml。
  • @JeffRSon 我不知道。我对 XDocuments 相当陌生,并且总体上积极地使用 XML。我不知道我必须用数字明确命名它们,但我可以想象如果你尝试使用相同名称创建多个键,字典会起作用。另外,我仍然需要知道如何为每个可以添加我找到的所有段落的结构。
  • 你能演示一下你的输入吗?我不清楚您如何表示构建“列表”元素之类的东西
  • @Nathan Err...我使用我得到的Dictionary,并获取所有称为“段落”的键。但是由于我可以在字典中包含多个段落,我们同意在其中添加数字。所以我可以得到paragraph0, paragraph1, paragraph2...paragraphN。意味着几乎无限数量的段落。但我永远不知道有多少。所以我必须以某种方式猜测。
  • 所以 Some PointSome other point 结构只是一个字符串?这让我很困惑。

标签: c# xml dictionary linq-to-xml


【解决方案1】:

使用 LinqToXML,这会将所有以“段落”开头的字典键放入您的文档中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

        var dict = new Dictionary<string, string>()
        {
             { "paragraph0", "asdflkj lksajlsakfdj laksdjf lksad jfsadf P0"},
             { "paragraph1", " alkdsjf laksdjfla skdfja lsdkfjadsflk P1"},
             { "paragraph2", "asdflkj lksajlsakfdj laksdjf lksad jfsadf P2"}
        };

        XDocument xd = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), 
            new XElement("document", 
                    dict.Where(kvp => kvp.Key.ToLower().StartsWith("paragraph"))
                        .Select(kvp => new XElement(kvp.Key, kvp.Value)),
                    new XElement("header", "etc")
                )
            );

【讨论】:

  • 它说System.Collections.Generic.Dictionary 不包含Where() 的定义
  • 哎呀,我自己才发现的。是的,现在没问题。我会测试代码并回复你。
  • 从我的测试应用添加了导入
  • @Nathan 是的,我尝试了您的解决方案,它成功了。非常感谢!
【解决方案2】:

嗯,第一段可以这样写:

Dictionary<String, String> dic = new Dictionary<String, String>();
dic.Add("title", "Some Title");
dic.Add("test", "a string");
dic.Add("paragraph0", "some text");
dic.Add("paragraph1", "some more text on a new line");
dic.Add("another test", "a string");
// -----
XDocument newXDoc = new XDocument();
newXDoc.Add(new XElement("document",
              new XElement("title", dic["title"]),
              dic.Where((kvp)=>kvp.Key.StartsWith("paragraph")).Select((kvp)=>new XElement("paragraph", dic[kvp.Key]))));

String s=newXDoc.ToString();
Console.WriteLine(s);

但是,第三段取决于您的输入。

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 2010-10-19
    • 2014-08-08
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多