StyleCop 使用 XDocument / XElement / XAttribute 愉快地创建 Xml

发布于 2024-09-07 15:13:29 字数 664 浏览 2 评论 0 原文

我喜欢使用以下格式创建 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?

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

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

发布评论

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

评论(1

潜移默化 2024-09-14 15:13:29

是的,我建议执行以下操作:

  • 为您的项目创建“默认资源”(右键单击项目、“属性”、“资源”)
  • 在那里创建一个新的字符串资源,将名称设置为 DefaultXmlDoc 或其他内容
  • 将值设置为以下文本:< br>
    <代码>
    <根目录>




  • 将您的程序更改为以下一行:
    <代码>
    XDocument xml = XDocument.Parse( Properties.Resources.DefaultXmlDoc );

我相信这可以实现您的所有目标。

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
  • Set the value as the following text:

    <Root>
    <A X="1" Y="2" />
    <B Z="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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文