Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用XML文本?_C#_Xml_Literals - Fatal编程技术网

C# 在C中使用XML文本?

C# 在C中使用XML文本?,c#,xml,literals,C#,Xml,Literals,是否可以在C代码文件中添加文字XML数据?我目前使用的是多行字符串文字,但正如您所看到的,它变得很混乱。有更好的方法吗 string XML = @"<?xml version=""1.0"" encoding=""utf-8""?> <customUI xmlns=""http://schemas.example.com/customui""> <toolbar id=""save""> </toolbar> </custo

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

string XML = @"<?xml version=""1.0"" encoding=""utf-8""?>
<customUI xmlns=""http://schemas.example.com/customui"">
    <toolbar id=""save"">
    </toolbar>
</customUI>";
是VB.NET的一个功能,而不是C

你所发布的内容与C语言中的内容非常接近

您可能想考虑用单引号替换嵌入的双引号,因为这两种类型都是有效的XML。


对于大量XML,您可能希望使用MARC——使用一个XML文件加载一次并存储在内存中的答案,这样您就可以利用XML编辑器。

< P>如果XML足够大,可以考虑使用平面.xml文件,或者从磁盘加载,或者作为资源嵌入。只要在静态构造函数中只加载一次,这对性能没有影响。它将更易于维护,因为它将使用IDE的XML文件编辑器。而且它不会妨碍您的代码。

关于我的评论,我记不起在哪里看到的,但我终于找到了答案

现在回想起来,LINQtoXML似乎是最好的选择。它比串联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

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

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

又一次,奇怪?对具体情况?对但它是有效的,并且给人很大的智慧。请注意,它还将给出一系列有效性警告,具体取决于文档验证版本

在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);

作为两种语言的开发人员,这是为数不多的几次我真正想要一个特定的VB.NET功能:它使在VB.NET中编写XML变得如此干净,而且比序列化快得多。我怀疑我是否会在实践中使用它,但它肯定是一种非常有创造性的方法。荣誉
XDocument document = xml.ToXDocument(new MyType() {
    PropertyA = "foo",
    PropertyB = "bar",
    PropertyC = "qux",
});
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);