0

我正在编写一个 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 循环中吗?

4

1 回答 1

2

从评论转移到实际帖子。

将 XDocument 和 XElement 与列表一起使用

我将List<T>单独处理,然后按如下方式创建文档:

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());
}
于 2013-08-15T03:17:21.913 回答