0

内部にこの形式のデータの400以上のレコードがあります:

<rs:data>
   <z:row ows_ID='360' ows_LinkTitle='GEI Survey data to Sharepoint' ows_Project_x0020_Priority='0' ows_AssignedTo='615;#Jeremy, Ron' ows_Status='In Progress' ows_Priority='(2) Normal' ows_DueDate='2012-04-27 00:00:00' ows_PercentComplete='0.700000000000000' ows_Modified='2012-04-30 10:44:15' ows_Alignment='TSS Delivery Mgmt' ows_SME='44;#Lewis, Clark' />

   <z:row ows_ID='378' ows_LinkTitle='Create back end and environment to support User demographic reporting' ows_Project_x0020_Priority='0' ows_AssignedTo='615;#Sam, Johns' ows_Status='In Progress' ows_Priority='(2) Normal' ows_DueDate='2012-05-11 00:00:00' ows_PercentComplete='0.800000000000000' ows_Modified='2012-05-09 13:50:17' ows_Alignment='Team Internal' ows_SME='7;#CORP\sscer;#9;#CORP\vreer' />

   <z:row ows_ID='249' ows_LinkTitle='Training Material to Muti Media' ows_AssignedTo='620;#Jenkins, Kristen' ows_Status='Not Started' ows_Priority='(2) Normal' ows_DueDate='2012-08-10 00:00:00' ows_PercentComplete='0' ows_Modified='2012-05-16 11:20:29' ows_Alignment='Diver Support' ows_SME='1;#CORP\vsswer;#7;#CORP\adder' />

</rs:data>

ows_ID、ows_LinkTitle などの値を格納する個々の配列を作成するにはどうすればよいですか?

foreach (activeItemData の System.Xml.XmlNode ノード)

        {
            if (node.Name == "rs:data")
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    if (node.ChildNodes[i].Name == "z:row")
                    {
                        string [] resultTitle;
     resultTitle = (node.ChildNodes[i].Attributes["ows_Title"].Value).ToArray();
                        Console.ReadLine(); 
                    } 
                } 
          } 
      }

そのスローエラーエラーは、resultTitle で型 'char[]' を 'string[]' に暗黙的に変換できません。どうすれば修正できますか? ありがとう。

やった

char[] resultTitle;
resultTitle = (node.ChildNodes[i].Attributes ["ows_Title"].Value).ToArray();
string s = new string(resultTitle);
Console.ReadLine();

["ows_Title"] のすべての値に対してどうすればよいですか。? ありがとう。

4

2 に答える 2

1

このコード

char[] resultTitle;
resultTitle = (node.ChildNodes[i].Attributes ["ows_Title"].Value).ToArray(); 
string s = new string(resultTitle);
Console.ReadLine();

文字列値を受け取り、文字配列に変換します。).ToArray()すぐに文字配列を文字列に変換します。

これは、追加のメモリ割り当てなしで同じことです

string s = node.ChildNodes[i].Attributes ["ows_Title"].Value;

ただし、私があなただったら、linq to xml を使用するだけです。私も終わりたいと思いますList<string>

XNamespace z = "#RowsetSchema";
List<string> list = (from row in xdoc.Descendants(z + "row")
                     select (string)row.Attribute("ows_LinkTitle")
                    ).ToList();

完全なサンプル

    static void Main(string[] args)
    {
        string xstring = @"<xml xmlns:rs='urn:schemas-microsoft-com:rowset'
                         xmlns:z='#RowsetSchema'>
                            <rs:data>
                                <z:row ows_ID='360' ows_LinkTitle='GEI Survey data to Sharepoint' ows_Project_x0020_Priority='0' ows_AssignedTo='615;#Jeremy, Ron' ows_Status='In Progress' ows_Priority='(2) Normal' ows_DueDate='2012-04-27 00:00:00' ows_PercentComplete='0.700000000000000' ows_Modified='2012-04-30 10:44:15' ows_Alignment='TSS Delivery Mgmt' ows_SME='44;#Lewis, Clark' />
                               <z:row ows_ID='378' ows_LinkTitle='Create back end and environment to support User demographic reporting' ows_Project_x0020_Priority='0' ows_AssignedTo='615;#Sam, Johns' ows_Status='In Progress' ows_Priority='(2) Normal' ows_DueDate='2012-05-11 00:00:00' ows_PercentComplete='0.800000000000000' ows_Modified='2012-05-09 13:50:17' ows_Alignment='Team Internal' ows_SME='7;#CORP\sscer;#9;#CORP\vreer' />
                               <z:row ows_ID='249' ows_LinkTitle='Training Material to Muti Media' ows_AssignedTo='620;#Jenkins, Kristen' ows_Status='Not Started' ows_Priority='(2) Normal' ows_DueDate='2012-08-10 00:00:00' ows_PercentComplete='0' ows_Modified='2012-05-16 11:20:29' ows_Alignment='Diver Support' ows_SME='1;#CORP\vsswer;#7;#CORP\adder' />
                          </rs:data> 
                          </xml>";



        XDocument xdoc = XDocument.Parse(xstring); // there are other ways to construct your xdoc
        XNamespace z = "#RowsetSchema";
        List<string> list = (from row in xdoc.Descendants(z + "row")
                            select  (string)row.Attribute("ows_LinkTitle")
                            ).ToList();

        foreach (var item in list)
            Console.WriteLine(item);

    }
于 2012-05-24T22:21:31.150 に答える
0

シンプルに使うだけ

char[] resultTitle;

それ以外の

string [] resultTitle;
于 2012-05-24T21:37:30.087 に答える