English 中文(简体)
C# 将xml分析成数组
原标题:C# Parse xml into arrays
  • 时间:2012-05-24 21:35:01
  •  标签:
  • c#
  • .net

我有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;#CORPsscer;#9;#CORPvreer  />

   <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;#CORPvsswer;#7;#CORPadder  />

</rs:data>

我如何创建非目性阵列来存储 ows_ ID, ows_ LinkTitle and 的值?

每一个( 运行中的项目数据中的 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(); 
                    } 
                } 
          } 
      }

丢弃错误 错误 错误 无法隐含地将类型字符 [] 转换为结果Tittle 的字符串 。 我如何纠正它? 谢谢 。

我做了,我做了

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

我该如何做呢?

最佳回答

此代码

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

需要一个字符串值转换成字符数组 。 < code>. toArray () 并立即将字符组转换为字符组 。

这是同样的事情,没有额外的记忆分配

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

如果我是你,我会使用 linq 到 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;#CORPsscer;#9;#CORPvreer  />
                               <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;#CORPvsswer;#7;#CORPadder  />
                          </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);

    }
问题回答

简单使用

char[] resultTitle;

代替

string [] resultTitle;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签