在 C# 中使用 XML 文字吗?

发布于 2024-12-25 19:35:40 字数 294 浏览 1 评论 0原文

是否可以在 C# 代码文件中添加文字 XML 数据?我目前正在使用多行字符串文字,但如您所见,它变得混乱。有更好的方法吗?

string XML = @"<?xml version=""1.0"" encoding=""utf-8""?>
<customUI xmlns=""http://schemas.example.com/customui"">
    <toolbar id=""save"">
    </toolbar>
</customUI>";

Is it possible to add literal XML data within a C# code file? I'm currently using a multiline string literal but it gets messy as you can see. Any better way of doing this?

string XML = @"<?xml version=""1.0"" encoding=""utf-8""?>
<customUI xmlns=""http://schemas.example.com/customui"">
    <toolbar id=""save"">
    </toolbar>
</customUI>";

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

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

发布评论

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

评论(5

无悔心 2025-01-01 19:35:40

XML 文字是 VB.NET 的功能,而不是 C# 的功能。

您发布的内容是您在 C# 中所能得到的最接近的内容。

不过,您可能需要考虑将嵌入的双引号替换为单引号(因为这两种类型都是有效的 XML)。

对于大量 XML,您可能需要考虑 Marc 的答案 - 使用 XML 文件(加载一次并存储在内存中),这样您就可以利用 XML 编辑器。

XML literals are a feature of VB.NET, not C#.

What you have posted is as close as you can get in C#.

You may want to consider replacing the embedded double quotes with single quotes though (as both types are valid XML).

For larger amounts of XML you may want to consider the answer from Marc - using an XML file (loaded once and stored in memory), so you can take advantage of the XML editor.

著墨染雨君画夕 2025-01-01 19:35:40

如果 XML 足够大而妨碍,请考虑使用平面 .xml 文件,从磁盘加载或作为资源嵌入。只要您只加载一次(可能在静态构造函数中),这对性能不会有任何影响。由于它将使用 IDE 的 XML 文件编辑器,因此维护起来会容易得多。而且它不会妨碍您的代码。

If the XML is big enough to get in the way, consider using a flat .xml file instead, either loaded from disk, or embedded as a resource. As long as you only load it once (perhaps in a static constructor) this will make no difference to performance. It will be considerably easier to maintain, as it will use the IDE's XML file editor. And it won't get in the way of your code.

绝對不後悔。 2025-01-01 19:35:40

参考我的评论,我不记得在哪里看到过这个,但我终于找到了 XmlBuilder链接

回想起来,Linq to XML 似乎是您最好的选择。它比连接 XML 字符串更干净、更快且更易于维护:

XNamespace ns = "http://schemas.example.com/customui";
XDocument doc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement(ns + "customUI",
                        new XElement(ns + "taskbar",
                            new XAttribute("id", "save"))
                    )
                );

var stringWriter = new StringWriter();
doc.Save(stringWriter); //Write to StringWriter, preserving the declaration (<?xml version="1.0" encoding="utf-16" standalone="yes"?>)
var xmlString = stringWriter.ToString(); //Save as string
doc.Save(@"d:\out.xml"); //Save to file

With reference to my comment, I couldn't recall where I saw this, but I finally found the XmlBuilder link.

In retrospect, it seems Linq to XML would be your best bet. It's cleaner, faster and more maintainable than concatenating XML strings:

XNamespace ns = "http://schemas.example.com/customui";
XDocument doc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement(ns + "customUI",
                        new XElement(ns + "taskbar",
                            new XAttribute("id", "save"))
                    )
                );

var stringWriter = new StringWriter();
doc.Save(stringWriter); //Write to StringWriter, preserving the declaration (<?xml version="1.0" encoding="utf-16" standalone="yes"?>)
var xmlString = stringWriter.ToString(); //Save as string
doc.Save(@"d:\out.xml"); //Save to file
梦中楼上月下 2025-01-01 19:35:40

作为一个特殊的、非常具体的解决方案,如果您碰巧在使用 Razor 引擎的 ASP.NET 环境中工作,在 CSHTML 文件中,您可以:

Func<MyType, HelperResult> xml = @<root>
    <item>@(item.PropertyA)</item>
    <item>@(item.PropertyB)</item>
    <item>@(item.PropertyC)</item>
</root>;

通过添加扩展方法:

public static XDocument ToXDocument<T>(this Func<T, HelperResult> source, T item)
{
    return XDocument.Parse(source(item).ToHtmlString());
}

然后您可以:

XDocument document = xml.ToXDocument(new MyType() {
    PropertyA = "foo",
    PropertyB = "bar",
    PropertyC = "qux",
});

再次,特殊? 。具体情况? 。但它确实有效,并且提供了很好的智能感知。 (请注意,它还会给出一堆有效性警告,具体取决于文档验证版本

As a peculiar, and very case-specific solution, if you happen to be working in an ASP.NET environment using the Razor engine, in a CSHTML file you can:

Func<MyType, HelperResult> xml = @<root>
    <item>@(item.PropertyA)</item>
    <item>@(item.PropertyB)</item>
    <item>@(item.PropertyC)</item>
</root>;

With the addition of an extension method:

public static XDocument ToXDocument<T>(this Func<T, HelperResult> source, T item)
{
    return XDocument.Parse(source(item).ToHtmlString());
}

You can then:

XDocument document = xml.ToXDocument(new MyType() {
    PropertyA = "foo",
    PropertyB = "bar",
    PropertyC = "qux",
});

Again, peculiar? Yes. Case-specific? Yes. But it works, and gives great Intellisense. (mind you, it also will give a bunch of validity warnings, depending on the document validation version)

晨与橙与城 2025-01-01 19:35:40

我们在 C# 中最接近的方式是通过 LINQ,类似这样:

var xml = XDocument.Load(
    new StringReader(@"<Books>
    <Book author='Dan Brown'>The Da Vinci Code</Book>
    <Book author='Dan Brown'>The Lost Symbol</Book>
    </Books>"));

var query = from book in xml.Elements("Books").Elements("Book")
    where book.Attribute("author").Value == "Dan Brown"
    select book.Value;

foreach (var item in query) Console.WriteLine(item);

The closest we could have in C# would be through LINQ, something like that:

var xml = XDocument.Load(
    new StringReader(@"<Books>
    <Book author='Dan Brown'>The Da Vinci Code</Book>
    <Book author='Dan Brown'>The Lost Symbol</Book>
    </Books>"));

var query = from book in xml.Elements("Books").Elements("Book")
    where book.Attribute("author").Value == "Dan Brown"
    select book.Value;

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