迭代 Umbraco getAllTagsInGroup 结果

发布于 2024-10-04 13:52:02 字数 1456 浏览 1 评论 0原文

我正在尝试使用以下代码从 Umbraco (v4.0.2.1) 中的特定标签组获取标签列表:

var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");

我想要做的只是输出这些标签的列表。但是,如果我输出变量“tags”,它只会输出字符串中所有标签的列表。我想将每个标签分成一个新行。

当我检查“tags”变量的数据类型时:

string tagType = tags.GetType().ToString();

...它输出 MS.Internal.Xml.XPath.XPathSelectionIterator

所以问题是,如何从“tags”变量中获取各个标签?如何使用这种数据类型的变量?我可以找到如何通过加载实际 XML 文件来完成此操作的示例,但我没有实际的 XML 文件 - 只有要使用的“tags”变量。

非常感谢您的帮助!

EDIT1:我想我要问的是,如何循环访问 XPathSelectionIterator 数据类型返回的节点?

EDIT2:我找到了这段代码,它几乎满足了我的需要:

        XPathDocument document = new XPathDocument("file.xml");
        XPathNavigator navigator = document.CreateNavigator();

        XPathNodeIterator nodes = navigator.Select("/tags/tag");
        nodes.MoveNext();
        XPathNavigator nodesNavigator = nodes.Current;

        XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

        while (nodesText.MoveNext())
            debugString += nodesText.Current.Value.ToString();

...但它期望将实际 XML 文件的 URL 加载到第一行。我的 XML 文件本质上是“tags”变量,而不是实际的 XML 文件。因此,当我将:

XPathDocument document = new XPathDocument("file.xml");

...替换为:

XPathDocument document = new XPathDocument(tags);

...时,它只是错误。

I'm trying to get a list of tags from a particular tag group in Umbraco (v4.0.2.1) using the following code:

var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");

What I want to do is just output a list of those tags. However, if I output the variable 'tags' it just outputs a list of all tags in a string. I want to split each tag onto a new line.

When I check the datatype of the 'tags' variable:

string tagType = tags.GetType().ToString();

...it outputs MS.Internal.Xml.XPath.XPathSelectionIterator.

So question is, how do I get the individual tags out of the 'tags' variable? How do I work with a variable of this data type? I can find examples of how to do it by loading an actual XML file, but I don't have an actual XML file - just the 'tags' variable to work with.

Thanks very much for any help!

EDIT1: I guess what I'm asking is, how do I loop through the nodes returned by an XPathSelectionIterator data type?

EDIT2: I've found this code, which almost does what I need:

        XPathDocument document = new XPathDocument("file.xml");
        XPathNavigator navigator = document.CreateNavigator();

        XPathNodeIterator nodes = navigator.Select("/tags/tag");
        nodes.MoveNext();
        XPathNavigator nodesNavigator = nodes.Current;

        XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

        while (nodesText.MoveNext())
            debugString += nodesText.Current.Value.ToString();

...but it expects the URL of an actual XML file to load into the first line. My XML file is essentially the 'tags' variable, not an actual XML file. So when I replace:

XPathDocument document = new XPathDocument("file.xml");

...with:

XPathDocument document = new XPathDocument(tags);

...it just errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-10-11 13:52:02

既然它是一个迭代器,我建议你迭代它。 ;-)

var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");

foreach (XPathNavigator tag in tags) {
  // handle current tag
}

Since it is an Iterator, I would suggest you iterate it. ;-)

var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");

foreach (XPathNavigator tag in tags) {
  // handle current tag
}
神仙妹妹 2024-10-11 13:52:02

我认为这个技巧效果更好一些。

问题是 getAllTagsInGroup 返回所有标签的容器,您需要获取其子级。

foreach( var tag in umbraco.editorControls.tags.library.getAllTagsInGroup("category").Current.Select("/tags/tag") )
{
     /// Your Code
}

I think this does the trick a little better.

The problem is that getAllTagsInGroup returns the container for all tags, you need to get its children.

foreach( var tag in umbraco.editorControls.tags.library.getAllTagsInGroup("category").Current.Select("/tags/tag") )
{
     /// Your Code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文