我喜欢使用以下格式创建 xml:
XDocument xml = new XDocument(
new XElement("Root",
new XElement("A",
new XAttribute("X", xValue),
new XAttribute("Y", yValue)),
new XElement("B",
new XAttribute("Z", zValue)),
new XElement("C")));
它看起来很容易阅读,并且有点像选项卡式 XML 文档(在我看来)。不过 StyleCop 对格式非常不满意。我收到很多这样的错误:
SA1116:如果方法参数位于单独的行上,则第一个参数必须从方法名称下方的行开始。
SA1118:参数跨越多个线。如果参数很短,请将整个参数放在一行上。否则,将参数的内容保存在临时变量中,并将临时变量作为参数传递。
我该怎么做才能让 StyleCop 满意并且代码可读?我知道我可以禁用 StyleCop 规则,但团队希望为所有非 XML 创建代码保留这些规则。我可以有选择地抑制以这种方式创建 XML 的每个方法中的规则,但这看起来很痛苦并且变得丑陋。有什么建议吗?
I like to create xml using the following formatting:
XDocument xml = new XDocument(
new XElement("Root",
new XElement("A",
new XAttribute("X", xValue),
new XAttribute("Y", yValue)),
new XElement("B",
new XAttribute("Z", zValue)),
new XElement("C")));
It seems easy to read and kinda flows like a tabbed XML document (in my opinion). StyleCop is very unhappy with the formatting though. I get a lot of these errors:
SA1116: If the method parameters are on separate lines, the first parameter must begin on the line beneath the name of the method.
SA1118: The parameter spans multiple lines. If the parameter is short, place the entire parameter on a single line. Otherwise, save the contents of the parameter in a temporary variable and pass the temporary variable as a parameter.
What can i do to keep StyleCop happy and the code readable? I know i can disable the StyleCop rules, but the team would like to keep those rules for all the non XML creation code. I can selectively suppress the rule in every method that creates XML in this way, but that seems like a pain and gets to be ugly. Any suggestions?
发布评论
评论(1)
是的,我建议执行以下操作:
<代码>
<根目录>
<代码>
XDocument xml = XDocument.Parse( Properties.Resources.DefaultXmlDoc );
我相信这可以实现您的所有目标。
Yes, I would suggest the following:
<Root>
<A X="1" Y="2" />
<B Z="3" />
<C />
</Root>
XDocument xml = XDocument.Parse( Properties.Resources.DefaultXmlDoc );
I believe this accomplishes all your goals.