简体   繁体   English

如何在StringList中获取字符串? C#

[英]How to get a String in a StringList ? C#

I have a small problem with my Exchange Appointment. 我的Exchange约会有一个小问题。 The idea is to sync a meeting from sharepoint calendar to the personal exchange calendar. 想法是将会议从共享点日历​​同步到个人交换日历。

I have this StringList in my CalendarItem Class: 我的CalendarItem类中有以下StringList:

   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 . Microsoft.Exchange.WebService.Data.Appointment我具有预定义的StingList Categories

What happens now is that I'm parsing through an XML File which looks like: 现在发生的是,我正在通过一个XML文件进行解析,如下所示:

<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: 然后,我将整个XML代码解析为字符串,然后检查名称是否存在:

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 . 该错误是因为m_Category = StringListx.Value = String

Does anyone has an idea how to solve this little issue? 有谁知道如何解决这个小问题?

I am not familiar with class StringList, but I guess it a Collection. 我对StringList类不熟悉,但是我猜它是一个Collection。 You can simply initialize a new StringList with the string inside it : 您可以简单地使用其中的字符串初始化一个新的StringList:

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

Assuming you are asking about this class , you'll see that its constructor accepts an IEnumerable<string> , so you can write: 假设您在询问此类 ,您将看到其构造函数接受IEnumerable<string> ,因此您可以编写:

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

StringList implements IEnumerable so you can also use the collection initializer syntax: StringList实现IEnumerable,因此您也可以使用集合初始化器语法:

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 这分两步创建了该类-首先创建一个空对象,然后为初始化程序中的每个项目调用Add

You should be able to create a new Stringlist with the desired values (StringList inherits from List(Of String), so it's quite straightforward: 您应该能够使用所需的值创建一个新的Stringlist(StringList 继承自List(Of String),因此非常简单:

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. 编译器将为您完成其余的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM