我有這樣的代碼:C#XML解析。需要得到文本
using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApp1
{
[XmlRoot(ElementName = "doc")]
public class Doc
{
[XmlElement(ElementName = "headline")]
public string Headline { get; set; }
}
static class Program
{
static void Main(string[] args)
{
Doc res;
var serializer = new XmlSerializer(typeof(Doc));
using (var reader = new StringReader(File.ReadAllText("test.xml")))
{
res = (Doc) serializer.Deserialize(reader);
}
Console.Out.WriteLine(res.Headline.ToString());
}
}
}
我test.xml
文件包含這樣的信息:
<doc>
<headline>AZERTY on the English <hlword>QWERTY</hlword> layout.
</headline>
</doc>
當我嘗試分析它,我有一個例外:
System.InvalidOperationException occurred
HResult=0x80131509
Message=There is an error in XML document (2, 35).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at ConsoleApp1.Program.Main(String[] args) in D:\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 24
Inner Exception 1:
XmlException: Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 2, position 35.
我需要從這些文件中獲得輸出爲AZERTY on the English <hlword>QWERTY</hlword> layout.
或AZERTY on the English QWERTY layout.
。我需要設置什麼類型的Doc
Headline
屬性來獲取這樣的文本(可能與調用ToString()
屬性)?
P.S.我在Visual Studio 2017中使用Windows 10創作者更新(15.3.3)
謝謝。有了這樣除了正常工作: \t'''[XMLTEXT] \t公共字符串內容 \t { \t \t的get => _content; \t \t set => _content + = value; \t} \t私人字符串_content;''' – AJIOB