简体   繁体   English

StyleCop使用XDocument / XElement / XAttribute快乐创建Xml

[英]StyleCop happy creation of Xml using XDocument / XElement / XAttribute

I like to create xml using the following formatting: 我喜欢使用以下格式创建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")));

It seems easy to read and kinda flows like a tabbed XML document (in my opinion). 它似乎很容易阅读,有点像标签式XML文档(在我看来)。 StyleCop is very unhappy with the formatting though. StyleCop对格式化非常不满意。 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. SA1116:如果方法参数位于单独的行上,则第一个参数必须从方法名称下面的行开始。

SA1118: The parameter spans multiple lines. SA1118:参数跨越多行。 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? 我能做些什么来保持StyleCop的快乐和代码的可读性? I know i can disable the StyleCop rules, but the team would like to keep those rules for all the non XML creation code. 我知道我可以禁用StyleCop规则,但团队希望为所有非XML创建代码保留这些规则。 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. 我可以选择性地抑制以这种方式创建XML的每个方法中的规则,但这看起来很痛苦并且变得丑陋。 Any suggestions? 有什么建议么?

Yes, I would suggest the following: 是的,我建议如下:

  • Create 'default resources' for your project (right-click the project, Properties, Resourced) 为项目创建“默认资源”(右键单击项目,属性,资源)
  • Create a new string resource there, set the Name as DefaultXmlDoc or something 在那里创建一个新的字符串资源,将Name设置为DefaultXmlDoc或其他东西
  • Set the value as the following text: 将值设置为以下文本:
    <Root>
    <AX="1" Y="2" />
    <BZ="3" />
    <C />
    </Root>
  • Change your program to the following one-liner: 将您的计划更改为以下单行:
    XDocument xml = XDocument.Parse( Properties.Resources.DefaultXmlDoc );

I believe this accomplishes all your goals. 我相信这可以实现你的所有目标。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM