0

I have 400+ records of data in this format inside :

<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>

how do I create indivisual array to store the values of ows_ID, ows_LinkTitle annd so on?

foreach (System.Xml.XmlNode node in activeItemData)

        {
            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(); 
                    } 
                } 
          } 
      }

Its throwing error Error Cannot implicitly convert type 'char[]' to 'string[]' at resultTitle. How do I correct it? Thanks.

I did

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

how do I do it for all the values of ["ows_Title"]. ? Thanks.

5
  • We have no idea what rs or z mean (other than being some xml namespaces). What is your real xml and what exactly do want to get out of it? Commented May 24, 2012 at 21:46
  • L.B- The real xml is the data returned from a web reference to a SharePoint list. I m thinking of getting arrays of all the values of the attributes like [ows_ID], [ows_Title] etc in indivisual arrays so that I can pass them as parameters to insert those values into my SQL table. If you want to take a look at what it looks like- stackoverflow.com/questions/10739535/… Commented May 24, 2012 at 21:53
  • No thanks I can not say that i am much interested in how it looks after seeing your problem was char[] :) Commented May 24, 2012 at 21:56
  • @L.B. actually the problem is .ToArray() Commented May 24, 2012 at 22:22
  • @ConradFrix My comment was referring the accepted answer ;) Commented May 24, 2012 at 22:26

2 Answers 2

1

This code

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

Takes a string value turns into a character array. ).ToArray() and then immediately converts the character array back into a string.

This is the same thing without the extra memory allocation

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

If I were you however I would just use linq to xml. I'd also want to end up with a List<string>

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

Complete sample

    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);

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Conrad Frix for your help. LINQ to XML is really nice thing to know.
0

Simple just use

char[] resultTitle;

instead of

string [] resultTitle;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.