C# 如何读取XML文件

C# 如何读取XML文件,c#,xml,C#,Xml,我有以下用于纸牌游戏的XML结构。我想把卡片的滴度和描述加载到两个数组中,我可以用它们来随机排列卡片 <Cards> <CardTitles> <Title>Some Title</Title> . . . . .

我有以下用于纸牌游戏的XML结构。我想把卡片的滴度和描述加载到两个数组中,我可以用它们来随机排列卡片

    <Cards>
      <CardTitles>
        <Title>Some Title</Title>
                   .
                   .
                   .
                   .
                   .
     </CardTitles>
     <CardDesc>
       <Desc>Some description</Desc>
     </CardDesc>
    </Cards>

一些头衔
.
.
.
.
.
一些描述
但无论我做什么或写什么代码,我都无法从正确的标记中获取实际文本。我得到的最接近的例子如下:


我知道我不应该要求完整的解决方案,但我只是被难住了。如果能帮我解决这个问题,那就太好了。

您可以使用更简单、更直接的XmlSerializer,而不是走XmlReader的路线

你会有这样的东西:

<Cards>
      <CardTitles>
        <Title>Some Title</Title>
     </CardTitles>
     <CardDesc>
       <Desc>Some description</Desc>
     </CardDesc>
</Cards>
然后使用
XmlSerializer.Deserialize
方法

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Cards));
StringReader inputStrReader = new StringReader(inputString);
Cards cards = (Cards)xmlSerializer.Deserialize(inputStrReader);

假设您在C:\temp中有一个名为sample.xml的xml文件,您可以使用LINQ To xml:

   XElement x = XElement.Load (@"c:\temp\Sample.xml");

   IEnumerable<string> titles = from title in x.Element("CardTitles").Elements()
                                select title.Value;
   IEnumerable<string> descriptions = from description in x.Element("CardDesc").Elements()
                                      select description.Value;
XElement x=XElement.Load(@“c:\temp\Sample.xml”);
IEnumerable titles=来自x.Element(“CardTitles”).Elements()中的标题
选择title.Value;
IEnumerable descriptions=来自x.Element(“CardDesc”).Elements()中的描述
选择description.Value;

您到底有什么问题?这方面肯定有上百个例子和问题topic@Patrick . 有,但我就是不能把我的耳朵包起来。我所要做的就是将所有标题标记的内部文本(例如)放入一个字符串数组中。如果这是确切的xml结构,您可能会得到一个错误。你有一个未关闭的标题标签和点在中间。这是无效的…@Patrick这些点只是为了说明标题标签比他一个X_X多。好的。这似乎是一个非常直截了当的解决方案,问题是这是否适用于unity3d项目,它是否可以在iOS和android上运行?
   XElement x = XElement.Load (@"c:\temp\Sample.xml");

   IEnumerable<string> titles = from title in x.Element("CardTitles").Elements()
                                select title.Value;
   IEnumerable<string> descriptions = from description in x.Element("CardDesc").Elements()
                                      select description.Value;