2017-10-13 83 views
0

我有這樣的代碼: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.。我需要設置什麼類型的DocHeadline屬性來獲取這樣的文本(可能與調用ToString()屬性)?

P.S.我在Visual Studio 2017中使用Windows 10創作者更新(15.3.3)

回答

2

錯誤是告訴你它不能將<headline>AZERTY on the English <hlword>QWERTY</hlword> layout.解析爲一個簡單的字符串,因爲它包含一個元素。這被稱爲混合類型。爲了解析這一點,你需要修改的XMLObject組成,以這樣的

[XmlRoot(ElementName = "doc")] 
public class Doc 
{ 
    [XmlElement(ElementName = "headline")] 
    public Headline Headline { get; set; } 
} 

public class Headline 
{ 
    [XmlText] 
    public string Content { get; set; } 

    [XmlElement(ElementName = "hlword")] 
    public string HlWord { get; set; } 
} 
+1

謝謝。有了這樣除了正常工作: \t'''[XMLTEXT] \t公共字符串內容 \t { \t \t的get => _content; \t \t set => _content + = value; \t} \t私人字符串_content;''' – AJIOB

0

,你得到一個錯誤是在標題元素的含量hlword標籤的原因。如果將內容包裝進去,內容不會被解析,而是按原樣讀取。

<doc> 
    <headline><![CDATA[AZERTY on the English <hlword>QWERTY</hlword> layout.]]></headline> 
</doc> 
+0

對不起,但我有這樣的答案從服務器(這只是它的一部分)。我無法更改我的XML – AJIOB