How to get a String in a StringList ? C#

448 Views Asked by At

I have a small problem with my Exchange Appointment. The idea is to sync a meeting from sharepoint calendar to the personal exchange calendar.

I have this StringList in my CalendarItem Class:

   private StringList m_Category;
   public StringList Category { get { return m_Category; } }

And there it should go:

Microsoft.Exchange.WebServices.Data.Appointment xApointment = new Appointment(m_Service)
xApointment.Categories = xItem.Category; //xItem = Instance of CalendarItem

In the Microsoft.Exchange.WebService.Data.Appointment I have the predefinied StingList Categories.

What happens now is that I'm parsing through an XML File which looks like:

<listitems  xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
    xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
    xmlns:rs="urn:schemas-microsoft-com:rowset" 
    xmlns:z="#RowsetSchema" 
    xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <rs:data ItemCount="1">
    <z:row  ows_ContentTypeId="0x010200C5A7325634A3154BB8249D1C36246E00" 
    ows_Title="Test GetAllViewFields" 
    ows_Location="Testhausen" 
    ows_EventDate="2014-12-15 08:00:00" 
    ows_EndDate="2014-12-15 09:00:00" 
    ows_Description="Ein Test meeting" 
    ows_fAllDayEvent="0" 
    ows_fRecurrence="0" 
    ows_EventType="0" 
    ows_RecurrenceID="2014-12-15 08:00:00" 
    ows_Duration="3600" 
    ows_ParticipantsPicker="" 
    ***ows_Category="Geschäftlich"*** 

And than I'm parsing this whole XML Code as a String and check if the name is there:

private void initFromXmlAttribute(System.Xml.Linq.XAttribute x)
    {
        string AttributeName = x.Name.LocalName.Replace("ows_", "");
        try
        {
            if
            (AttributeName == "Category")
            {
                m_Category= x.Value; //Error implicite convert not possible StringList to String
                return;
            }

The Error is because m_Category = StringList and x.Value = String.

Does anyone has an idea how to solve this little issue?

3

There are 3 best solutions below

1
On BEST ANSWER

I am not familiar with class StringList, but I guess it a Collection. You can simply initialize a new StringList with the string inside it :

m_Category = new StringList() { x.Value };
1
On

Assuming you are asking about this class, you'll see that its constructor accepts an IEnumerable<string>, so you can write:

m_Category=new StringList(new[]{x.Value});

StringList implements IEnumerable so you can also use the collection initializer syntax:

m_Category=new StringList{x.Value};

This creates the class in two steps - first creates an empty object and then calls Add for each item in the initializer

1
On

You should be able to create a new Stringlist with the desired values (StringList inherits from List(Of String), so it's quite straightforward:

myStringList = new StringList()
myStringList.add(x.Value)

Btw since you are effectively setting your "Category" Property you might want to consider including a "set" statement to your property instead of modifying your private variable directly. In fact you can skip defining your private variable completely by using:

public StringList Category { get; set; }

Category = new StringList()

The compiler will do the rest for you.