c# - 无需 Linq 即可解析和访问 XML

标签 c# xml xml-parsing

我使用的平台允许 XML,但不允许 Linq,因此我必须在没有 XElement 和 XDocument 的情况下解析 XML,那么我应该使用什么来代替这两个呢? 我还找到了一个代码,如果有人可以解释我如何转换它

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

最佳答案

您需要使用 System.Xml.XmlDocument 而不是 System.Xml.Linq.XDocument。此外,您需要使用 XPath 来查找指定的元素。如果您不熟悉 XPath,学习起来会有点复杂;相反,请检查此链接:该帖子的答案提供了一种访问 XML 属性的非常简单的方法: Read XML Attribute using XmlDocument

关于c# - 无需 Linq 即可解析和访问 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891895/

相关文章:

c# - 使用 XmlSerializer 反序列化 XML

c# - 为除根节点之外的所有元素添加 Xml 属性

python - 不明白为什么一个 XML 字段会出现 AttributeError 而其他字段却不会

java - ROME 0.8 解析异常

c# - Xamarin - 找不到元数据文件 .../AWSSDK.SecurityToken.CodeAnalysis.dll

c# - 为什么Object.Equals优先于从实例调用String.Equals?

c# - 在数组上使用 System.Linq 中的 .Any 查找字符串

c# - 在 ForEach 函数中模拟 break 语句

java - map View : Could not find class A referenced from method B

xml - javax.xml.bind.UnmarshalException : unexpected element (uri :"", 本地 :"sRAssignments")。预期的元素是