LINQ to XML语法 - c#

我有一个简单的POCO类,用于保存从XML文件提取的数据,该XML文件定义如下:

public class Demographics
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string Gender { get; set; }
}

我有一个相当简单的XML文件(在这种情况下,是元素)从中提取数据,定义如下:

<patient determinerCode="INSTANCE">
    <name use="L">
        <given>John</given>
        <given qualifier="IN">Q.</given>
        <family>Public</family>
    </name>
    <administrativeGenderCode code="F" displayName="Female"/>   
</patient>

我面临的挑战是将中间的名字首字母和/或名字放入类中的正确属性中。如您所见,名称节点内有两个给定的节点,中间的首字母由“ IN”属性指定。我是否在这里缺少简单的LINQ语法,还是需要查询所有给定的节点并枚举它们以正确放置每个节点?

我目前的代码如下所示:

private string GetInterfaceXmlData(XElement source)
{
        //Source in this context represents the "patient" element as you see in the example.
        //The NMSPC constant represents the namespace for this XML document which is not specifically displayed
        //in the XML example.
        Demographics currentDemo = new Demographics()
        {
            //I realize that this particular reference to FirstName is not optimal, as it may not actually
            //be the first "given" node under the name node, it's simply a place holder for now.
            FirstName = source.Element(NMSPC + "name").Element(NMSPC + "given").Value,
            LastName = source.Element(NMSPC + "name").Element(NMSPC + "family").Value,
            Gender=source.Element(NMSPC+"administrativeGenderCode").Attribute("code").Value,
        };
        XElement result = new XElement("XML");
        result.Add(new XElement("Demographics"));
        return result.ToString();
}

参考方案

怎么样:

// For the first name
source.Element(NMSPC + "name")
      .Elements(NMSPC + "given")
      .Where(element => element.Attribute("IN") == null)
      .First()

// For the initial
source.Element(NMSPC + "name")
      .Elements(NMSPC + "given")
      .Where(element => element.Attribute("IN") != null)
      .First()

编辑:查询语法在这里有点尴尬。对于第一个版本,它将是:

(from element in .Element(NMSPC + "name").Elements(NMSPC + "given")
where element.Attribute("IN") == null
select element).First()

我个人会坚持使用点表示法。

如何处理LINQ to Entities仅支持无参数的构造函数和初始化程序[重复] - c#

This question already has answers here: Only parameterless constructors and initializers are supported in LINQ to Entities (14个回答) 4年前关闭。 因此,当我尝试从Web Api获取数据时会发生这种情况:ExceptionMessa…

您可以在选择项目之前在linq查询中设置属性吗? - c#

有什么方法可以在查询中间设置属性?var products = from p in Products let pricecalc = p.IsDiscontinued ? 0 : p.UnitPrice // somehow set p.Price = pricecalc select p; 我知道我可以使用select new Product { .. s…

LINQ中的动态查询 - c#

我在Web表单中有四个按钮,它们的目的分别是加载第一条,上一条,下一条和最后一条记录。他们通过获取表单中加载的当前ID并根据需要执行操作。我正在使用Linq to SQL。下面是我用于NEXT记录的代码,如果我想通过ID加载下一条记录,则该函数运行良好。var dx = new DataModelDataContext(); List<Dog> …

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

Flask to Dygraph-如何传递数据? - javascript

如果我有一个简单的Python时间数据系列,例如:graphdata = [] graphdata.append( [(datetime.date(2008, 5, 7)),75]) graphdata.append([(datetime.date(2008, 5, 8)), 85]) graphdata.append([(datetime.date(200…