Use XML Literals in C#?

2019-01-23 11:16发布

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>";

标签: c# xml literals
5条回答
Summer. ? 凉城
2楼-- · 2019-01-23 11:27

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);
查看更多
姐就是有狂的资本
3楼-- · 2019-01-23 11:37

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)

查看更多
We Are One
4楼-- · 2019-01-23 11:40

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.

查看更多
三岁会撩人
5楼-- · 2019-01-23 11:46

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.

查看更多
男人必须洒脱
6楼-- · 2019-01-23 11:52

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
查看更多
登录 后发表回答