0

私は xmlStringを書いていますが、2 つの異なるものを渡しています。XML の書き込みを続行するには、のアイテムがLists必要です。LoopList

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 のリストと storage Path のリスト内のすべての項目が 1 つの文字列に格納されています。私の現在のケースでは、返された 3 つの XML 文字列が必要です。リストをループする for ループに XML を配置する必要がありますか?

4

1 に答える 1

2

コメントから実際の投稿に移動します。

List での 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 に答える