CustomXmlPropertyCollection.Item

CustomXmlPropertyCollection indexer (1 of 2)

获取具有指定名称的属性。

public CustomXmlProperty this[string name] { get; }
范围 描述
name 要查找的属性的区分大小写的名称。

例子

展示如何使用智能标签属性来获取有关智能标签的深入信息。

Document doc = new Document(MyDir + "Smart tags.doc");

// 智能标记出现在文档中,Microsoft Word 将其文本的一部分识别为某种形式的数据,
// 例如姓名、日期或地址,并将其转换为显示紫色虚线下划线的超链接。
// 在 Word 2003 中,我们可以通过“工具”启用智能标签 -> “自动更正选项...” -> “智能标签”。
// 在我们的输入文档中,Microsoft Word 将三个对象注册为智能标记。
// 智能标签可能是嵌套的,所以这个集合包含更多。
SmartTag[] smartTags = doc.GetChildNodes(NodeType.SmartTag, true).OfType<SmartTag>().ToArray();

Assert.AreEqual(8, smartTags.Length);

// 智能标签的“属性”成员包含其元数据,对于每种类型的智能标签,元数据会有所不同。
// “日期”类型智能标签的属性包含它的年、月和日。
CustomXmlPropertyCollection properties = smartTags[7].Properties;

Assert.AreEqual(4, properties.Count);

using (IEnumerator<CustomXmlProperty> enumerator = properties.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        Console.WriteLine($"Property name: {enumerator.Current.Name}, value: {enumerator.Current.Value}");
        Assert.AreEqual("", enumerator.Current.Uri);
    }
}

// 我们还可以通过各种方式访问属性,例如键值对。
Assert.True(properties.Contains("Day"));
Assert.AreEqual("22", properties["Day"].Value);
Assert.AreEqual("2003", properties[2].Value);
Assert.AreEqual(1, properties.IndexOfKey("Month"));

// 下面是从属性集合中移除元素的三种方法。
// 1 - 按索引删除:
properties.RemoveAt(3);

Assert.AreEqual(3, properties.Count);

// 2 - 按名称删除:
properties.Remove("Year");

Assert.AreEqual(2, properties.Count);

// 3 - 一次清除整个集合:
properties.Clear();

Assert.AreEqual(0, properties.Count);

也可以看看


CustomXmlPropertyCollection indexer (2 of 2)

获取指定索引处的属性。

public CustomXmlProperty this[int index] { get; }
范围 描述
index 属性的从零开始的索引。

例子

展示如何使用智能标签属性来获取有关智能标签的深入信息。

Document doc = new Document(MyDir + "Smart tags.doc");

// 智能标记出现在文档中,Microsoft Word 将其文本的一部分识别为某种形式的数据,
// 例如姓名、日期或地址,并将其转换为显示紫色虚线下划线的超链接。
// 在 Word 2003 中,我们可以通过“工具”启用智能标签 -> “自动更正选项...” -> “智能标签”。
// 在我们的输入文档中,Microsoft Word 将三个对象注册为智能标记。
// 智能标签可能是嵌套的,所以这个集合包含更多。
SmartTag[] smartTags = doc.GetChildNodes(NodeType.SmartTag, true).OfType<SmartTag>().ToArray();

Assert.AreEqual(8, smartTags.Length);

// 智能标签的“属性”成员包含其元数据,对于每种类型的智能标签,元数据会有所不同。
// “日期”类型智能标签的属性包含它的年、月和日。
CustomXmlPropertyCollection properties = smartTags[7].Properties;

Assert.AreEqual(4, properties.Count);

using (IEnumerator<CustomXmlProperty> enumerator = properties.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        Console.WriteLine($"Property name: {enumerator.Current.Name}, value: {enumerator.Current.Value}");
        Assert.AreEqual("", enumerator.Current.Uri);
    }
}

// 我们还可以通过各种方式访问属性,例如键值对。
Assert.True(properties.Contains("Day"));
Assert.AreEqual("22", properties["Day"].Value);
Assert.AreEqual("2003", properties[2].Value);
Assert.AreEqual(1, properties.IndexOfKey("Month"));

// 下面是从属性集合中移除元素的三种方法。
// 1 - 按索引删除:
properties.RemoveAt(3);

Assert.AreEqual(3, properties.Count);

// 2 - 按名称删除:
properties.Remove("Year");

Assert.AreEqual(2, properties.Count);

// 3 - 一次清除整个集合:
properties.Clear();

Assert.AreEqual(0, properties.Count);

也可以看看