【问题标题】:looping through list of items to produce xml result循环遍历项目列表以生成 xml 结果
【发布时间】:2013-08-15 02:17:00
【问题描述】:

我正在编写一个 xml String 并传入两个不同的 Lists 并且我想 Loop List 中的项目继续编写 XML。

public class Results
        {
            public List<Guid> itemGuid { get; set; }
            public List<string> storagePath { get; set; }
            public int userId {get; set;}
        }

public void CreateOutput(string inFile, string storagePath, int userId)
        {
            Results results = GetfileInfo(inFile, storagePath);for each of the pages
            CheckinXml(results.itemGuid, results.storagePath , userId); //

        }

public string CheckinXml(List<Guid> itemGuid, List<string> storagePath, int userId)
        {
            XDocument xdoc = new XDocument();
                 xdoc = new XDocument(
                    new XElement("MyList",
                        new XElement("Record",
                            new XElement("ID", itemGuid),
                            new XElement("StoragePath", storagePath),
                            new XElement("UploadedUserID", userId)
                            )
                    )
                );

            string result = xdoc.ToString();
            return result;
        }

目前itemGuid列表和存储路径列表中的所有项目都存储在一个字符串中。在我目前的情况下,我应该返回三个 XML 字符串。我应该将 XML 放在循环列表中的 for 循环中吗?

【问题讨论】:

  • 你能提供更多关于用例的信息吗?可以使用 XmlSerializer 将对象转换为 XML,反之亦然。它非常理想,因为在 C# 中您可以处理对象,然后将其保存到 XML 以供以后使用。
  • 不确定这是否有帮助,只是一个建议。 Guid和存储路径有关系吗?如果是,我建议您创建一个具有三个属性 Guid、storagepath 和 userid 的类,然后使用 Xml Serializer 为您完成这项工作。
  • @Justin 示例:客户提交一个文件,我读取文件的每一页,其中每一页都包含自己的 id 和存储位置以及处理它的人的 id。
  • @Nilesh 因为我在另一个方法中调用它,我应该将此方法放在 for 循环中吗?
  • @Masriyah XElement 有重载的 CTOR。其中一个可以采用数组,因此您可以使用列表而不是循环,可以使用ToArray() 方法将其转换为数组。

标签: c# xml c#-4.0


【解决方案1】:

从 cmets 转到实际帖子。

在列表中使用 XDocument 和 XElement

我会单独处理List&lt;T&gt;,然后这样创建文档:

public string CheckinXml(List<Guid> itemGuid, List<string> storagePath, int uploadUserId)
    {
        var guids = itemGuid.Select(i => new XElement("ID", i)).ToArray();
        var paths = storagePath.Select(i => new XElement("StoragePath", i)).ToArray();

        XDocument xdoc = new XDocument();
        xdoc = new XDocument(
           new XElement("MyList",
               new XElement("Record",
                   new XElement("IDs", guids),
                   new XElement("StoragePaths", paths),
                   new XElement("UploadedUserID", uploadUserId)
                   )
           )
       );

如果我通过以下调用调用它,则使用它:

var guids = new List<Guid> { new Guid(), new Guid(), new Guid() };
var paths = new List<string> { @"C:\home", @"D:\home", @"E:\home" };
var userId = 1000;

Console.WriteLine(CheckinXml(guids, paths, userId));

输出如下:

<MyList>
  <Record>
    <IDs>
      <ID>00000000-0000-0000-0000-000000000000</ID>
      <ID>00000000-0000-0000-0000-000000000000</ID>
      <ID>00000000-0000-0000-0000-000000000000</ID>
    </IDs>
    <StoragePaths>
      <StoragePath>C:\home</StoragePath>
      <StoragePath>D:\home</StoragePath>
      <StoragePath>E:\home</StoragePath>
    </StoragePaths>
    <UploadedUserID>1000</UploadedUserID>
  </Record>
</MyList>

现在您的列表已正确显示。


使用 XmlSerializer 的示例

可能有些人不喜欢,但是可以使用属性来确定xml节点名称是如何定义的

[XmlRoot(ElementName = "Record")]
public class Results
{
    [XmlArray(ElementName = "Ids"), XmlArrayItem(ElementName = "Id")]
    public List<Guid> itemGuid { get; set; }

    [XmlArray(ElementName = "StoragePaths"), XmlArrayItem(ElementName = "StoragePath")]
    public List<string> storagePath { get; set; }

    [XmlElement(ElementName = "UploadedUserId")]
    public int userId { get; set; }
}

然后使用下面的代码,就可以序列化对象了:

var results = new Results
    {
      itemGuid = new List<Guid> {new Guid(), new Guid(), new Guid()},
      storagePath =  new List<string>{@"C:\home\", @"D:\home\", @"E:\home\"},
      userId = 1234,
    };

var serializer = new XmlSerializer(results.GetType());

using (var sw = new StringWriter())
{
    serializer.Serialize(sw, results);

    Console.WriteLine(sw.ToString());
}

【讨论】:

  • 哇,太好了,我会继续尝试一下。我可能会改变格式,这样对于每条记录,我都会有一个 id、一个路径和用户 id。
  • @Masriyah 我只是使用您提供的格式。如果您提供了一个示例输出,它将有助于获得您想要的内容,但由于您有两个列表,您可能会感到头疼。例如,您如何知道哪个路径指向哪个项目 id?如果它们以某种方式关联,则使用 class 来表示它或使用 Dictionary&lt;Guid, string&gt; 会比两个列表并尝试建立关联要好。
  • 我的 guid 和字符串来自一个返回结果的类
  • @Masriyah 我建议发布整个课程,然后看看你的结构如何。正如前面的 cmets 所述,也许使用 XmlSerializer 更适合您想要完成的任务。
  • 非常感谢您的帮助和时间。我在上面添加了更多关于类的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多