【发布时间】: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()方法将其转换为数组。