【问题标题】:Load specific XML attributes/values加载特定的 XML 属性/值
【发布时间】:2013-07-17 23:32:25
【问题描述】:

我已经尝试了 2 天来理解 C# 中的 LINQ to XML,但是,我搞砸了,因为我从未见过像另一个教程那样的教程,我的意思是它们都是不同的,因为我是C# 初学者,我只需要在我的游戏服务器端使用 C# 我想知道如何

  1. 访问名称(属性)为“MyChar”的技能 1 类型(属性)
  2. 访问 name(att) 为“MyChar1”的技能 1 的描述

<chars>
    <character name="MyChar1">
        <skill1 type="attack" damage="30">
            description of skill1
            <name>Skill name</name>
            <class1 type="The Class Type"></class1>
            <class2 type="The Class Type 2"></class2>
        </skill1>
         //Almost same thing till skill4    
        <skill2>...</skill4>
    <character name="MyChar2">
        <skill1 type="attack" damage="30"></skill1>
        <skill2>....</skill4>
    </character>
</chars>

【问题讨论】:

  • 您似乎缺少&lt;/character&gt;

标签: c# .net xml linq


【解决方案1】:

试试这个代码:

// parse the document (this is your doc but I've made the xml parseable)
var doc = XDocument.Parse(@"<chars>
        <character name=""MyChar1"">
        <skill1 type=""attack"" damage=""30"">
            description of skill1
            <name>Skill name</name>
            <class1 type=""The Class Type""></class1>
            <class2 type=""The Class Type 2""></class2>
        </skill1>
        </character>
    <character name=""MyChar2"">
        <skill1 type=""attack"" damage=""30""></skill1>
    </character>
</chars>");

// Access a skill1 type(attribute) where the name(attribute) is "MyChar" 
// this is pretty easy with LINQ. We first get all descendant nodes of type "character"
var skillWhereNameIsMyChar1 = doc.Descendants("character")
    // then take the single one with an attribute named "value"
    .Single(ch => ch.Attribute("name") != null && ch.Attribute("name").Value == "MyChar1")
    // and take that element's child element of type skill1 
    .Element("skill1");
// this will print <skill1 ... /skill1>. However, this is an XElement object, not a string
// so you can continue to access inner text, attributes, children etc.
Console.WriteLine(skillWhereNameIsMyChar1);

// 2. Access the description of skill1 where name(att) is "MyChar1"
// this is tricky because the description text is just floating among other tags
// if description were wrapped in <description></description>, this would be simply
// var description = skillWhereNameIsMyChar1.Element("description").Value;
// here's the hacky way I found to get it in the current xml:

// first get the full value (inner text) of the skill node (includes "Skill Name")
var fullValue = skillWhereNameIsMyChar1.Value;
// then get the concatenated full values of all child nodes (= "Skill Name")
var innerValues = string.Join("", skillWhereNameIsMyChar1.Elements().Select(e => e.Value));
// get the description by dropping off the trailing characters that are actually inner values
// by limiting the length to the full length - the length of the non-description characters
var description = fullValue.Substring(0, length: fullValue.Length - innerValues.Length);
Console.WriteLine(description);

【讨论】:

  • 感谢您的快速回复,顺便说一句 var doc = XDocument.Load(XMLfile) 会以同样的方式工作吗? Skill1WhereNameIsMyChar1 的输出是什么?你能解释一下吗? Substring(0, fullValue.Length - innerValues.Length);
  • @IgliKadija:是的,会的
  • 正如我所见,制作 很简单:P
  • 嘿伙计,“访问技能1类型(属性)例如攻击”怎么样
  • @IgliKadija:就做skillWhereNameIsMyChar1.Attribute("type").Value
猜你喜欢
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多