简体   繁体   English

根据另一个 xml 属性的值使用 XDocument 获取 xml 属性

[英]Get xml attribute with XDocument based on value of another xml attribute

I have some xml that looks something like:我有一些 xml 看起来像:

<forms>
    <form name="admin" title="Admin Info">
        <field name="primary" label="Primary Name" required="false">
        <group desc="General" name="personalinfo" required="false" hide="false">
            <field label="Photos" name="photoupload" required="false" hide="false">
            <field label="First Name" name="firstanme" required="false" hide="false">
        </group>
    </form>
    <form name = "..." etc>
        ....etc...
    </form>
</forms>

I am trying to get information from the inner "field" tag.我试图从内部“字段”标签中获取信息。 For example, I want to get the "required" and "hide" values when name="photoupload."例如,我想在 name="photoupload." 时获得“必需”和“隐藏”值。

What I have so far:到目前为止我所拥有的:

        XDocument doc = XDocument.Parse(xmlTemplate);
        var photoInfo = doc.Descendants("field")
                                .Where(field => field.Attribute("name").Value == "photoupload")
                                .Select(field => new
                                {
                                    Hide = field.Attribute("hide").Value,
                                    Required = field.Attribute("required").Value
                                })
                                .Single();

        photoInfoTextBox.Text = photoInfo.Hide.ToString();

However, I am getting an " Object reference not set to an instance of an object. " error.但是,我收到“ Object reference not set to an instance of an object. ”错误。 My guess is that the code is trying to get info from the first "field" tag (where name="primary"), but actually I want info from the inner field tag, specifically:我的猜测是代码试图从第一个“字段”标签(其中 name="primary")获取信息,但实际上我想要来自内部字段标签的信息,特别是:

forms/form(where name="admin")/group(where desc="general")/field(where name="photoupload").表单/表单(其中名称=“管理员”)/组(其中描述=“通用”)/字段(其中名称=“照片上传”)。

How would I go about doing this?我该怎么做呢?

Just use casting instead of reading Value property:只需使用强制转换而不是读取Value属性:

    var photoInfo = doc.Descendants("field")
                       .Where(field => (string)field.Attribute("name") == "photoupload")
                       .Select(field => new {
                                Hide = (bool?)field.Attribute("hide"),
                                Required = (bool?)field.Attribute("required")
                       })
                       .Single();

Most likely you have an exception because some field element don't have name attribute.很可能你有一个例外,因为某些field元素没有name属性。 That means field.Attribute("name") will return null .这意味着field.Attribute("name")将返回null And null.Value will throw NullReferenceException .null.Value将抛出NullReferenceException Note that some elements also don't have hide attribute.请注意,某些元素也没有hide属性。

When you cast XAttribute or XElement to type which can have null value, you will get null if attribute or element do not exist.当您将XAttributeXElement转换为可以具有null值的类型时,如果属性或元素不存在,您将获得null值。 No exceptions will be thrown.不会抛出任何异常。

NOTE: Thus you have only one field with given name, you can simply try get that field注意:因此您只有一个具有给定名称的field ,您可以简单地尝试获取该字段

var photoUpload = doc.Descendants("field")
                     .Single(f => (string)f.Attribute("name") == "photoupload");

// or
var photoUpload = doc.XPathSelectElement("//field[@name='photoupload']");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM