简体   繁体   English

如何使用 ExCSS Parser 将 StyleRules 作为字符串输出

[英]How to get StyleRules as a string output using ExCSS Parser

I'm using ExCSS to parse and manipulate a stylesheet string.我正在使用ExCSS来解析和操作样式表字符串。 So far so good.到现在为止还挺好。

But I can't find any documentation on how to convert the manipulated style rules into a string.但是我找不到任何关于如何将操纵的样式规则转换为字符串的文档。

Although the code may not be relevant to this question, this is what I'm doing:虽然代码可能与这个问题无关,但这就是我正在做的:

private string ManipulateCSS(string styles)
{
    ExCSS.Parser parser = new ExCSS.Parser();
    var stylesheet = parser.Parse(styles);

    // here I perform specific manipulations 
    // which are not relevant to this question...
    stylesheet.StyleRules
                  .SelectMany(r => r.Declarations)
                  .Where(d => d.Name == "<something>"
                  ...

    ...

    // Now, the next line is where I'm having issues: 
    // how to return the whole string with styles out of this ExCSS parser?
    return stylesheet.StyleRules.ToString();
}

Thank you for your help!感谢您的帮助!

Turns out the ToString() method needs to be called on the ExCSS.StyleSheet instance and I was calling it on the StyleRules collection.结果证明需要在 ExCSS.StyleSheet 实例上调用ToString()方法,而我在 StyleRules 集合上调用它。

You only need to do the following (as per my sample code in the question above):您只需要执行以下操作(按照我在上面问题中的示例代码):

return stylesheet.ToString();

I hope this answer might save someone else's time.我希望这个答案可以节省别人的时间。

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

相关问题 ExCSS中的StyleRules属性发生了什么? - What happened to the StyleRules property in ExCSS? c#使用ExCSS解析CSS - c# parse css using ExCSS 如何使用CodeDomProvider获取输出? - How to Get Output using CodeDomProvider? 在不使用html解析器的情况下,有一种方法,使用普通的字符串方法来获取两个指定字符串之间的字符串部分,在我的情况下为标签 - Without using a html parser is there a way, using an ordinary string method to get the part of a string between two specified strings, in my case tags 使用命令行解析器解析字符串 - Parsing a string using command Line parser STRING:如何从此输入字符串获取字符串输出? - STRING: how to get string output from this input string? 如何使用正则表达式匹配同一字符串上的两个条件以获得一个输出 - How to match two condition on same string to get one output using regex 如何使用antrl4 lexer和解析器在SQL Server存储过程文本中获取查询? - How can I get queries in a SQL Server stored procedure text using antrl4 lexer and parser? 从字符串中删除/后如何通过修改字符串来获得所需的输出 - How to get the desired output by modifying string after removing / from string 通用类字段解析器到字典 <String, String> 使用反射 - Universal classes field parser to Dictionary<String, String> using reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM