如何在字符串列表中获取字符串?C#.

本文关键字:字符串 获取 列表 | 更新日期: 2023-09-27 18:33:53

我的交流预约有一个小问题。这个想法是将会议从 SharePoint 日历同步到个人交换日历。

我的日历项类中有这个字符串列表:

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

它应该在那里:

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

Microsoft.Exchange.WebService.Data.Appointment我有预定义的StingList Categories.

现在发生的事情是我正在解析一个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"*** 

然后我将整个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;
            }

该错误是因为 m_Category = StringListx.Value = String

有没有人知道如何解决这个小问题?

如何在字符串列表中获取字符串?C#.

我不熟悉类StringList,但我想它是一个集合。您可以简单地初始化一个新的 StringList,其中包含字符串:

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

假设您正在询问此类,您将看到它的构造函数接受 IEnumerable<string> ,因此您可以编写:

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

StringList 实现了 IEnumerable,因此您还可以使用集合初始值设定项语法:

m_Category=new StringList{x.Value};

这将分两步创建类 - 首先创建一个空对象,然后为初始值设定项中的每个项调用 Add

你应该能够创建一个新的字符串列表,其中包含所需的值(StringList继承自List(Of String),所以它非常简单:

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

顺便说一句,由于您正在有效地设置"类别"属性,因此您可能需要考虑在您的属性中包含"set"语句,而不是直接修改私有变量。实际上,您可以使用以下命令完全跳过定义私有变量:

public StringList Category { get; set; }
Category = new StringList()

编译器将为您完成其余的工作。