XLINQ:xml 中缺少元素

发布于 2024-08-22 06:09:29 字数 730 浏览 3 评论 0原文

我有以下 xml

<students>
  <student>
    <id>12</id>
    <name>Mohsan</name>
  </student>
  <student>
    <id>2</id>    
  </student>
  <student>
    <id>3</id>
    <name>Azhar</name>
  </student>
</students>

注释,其中缺少 2 个名称元素。

我必须使用 Linq to XML 读取此 xml

我使用以下代码来获取所有学生..

请建议我改进此代码

var stds = from std in doc.Descendants("student")
                select new
                {
                    ID = std.Element("id").Value,
                    Name = (std.Element("name")!=null)?std.Element("name").Value:string.Empty
                };

i have the following xml

<students>
  <student>
    <id>12</id>
    <name>Mohsan</name>
  </student>
  <student>
    <id>2</id>    
  </student>
  <student>
    <id>3</id>
    <name>Azhar</name>
  </student>
</students>

note that in 2 name element is missing.

i have to read this xml using Linq to XML

i used following code to get all students..

please suggest me improvement in this code

var stds = from std in doc.Descendants("student")
                select new
                {
                    ID = std.Element("id").Value,
                    Name = (std.Element("name")!=null)?std.Element("name").Value:string.Empty
                };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-08-29 06:09:29

您可以利用以下事实:存在 来自 XElement 的显式转换到 string,对于 null XElement 引用返回 null。然后,您可以使用 null 合并运算符从 null 变为空字符串:

var stds = from std in doc.Descendants("student")
           select new
           {
               ID = std.Element("id").Value,
               Name = (string) std.Element("name") ?? "";
           };

You can use the fact that there's an explicit conversion from XElement to string, which returns null for a null XElement reference. You can then use the null-coalescing operator to go from null to an empty string:

var stds = from std in doc.Descendants("student")
           select new
           {
               ID = std.Element("id").Value,
               Name = (string) std.Element("name") ?? "";
           };
故人如初 2024-08-29 06:09:29

使用“let”的语法可以避免两次询问 Element("name")

var stds = from std in doc.Descendants("student")
            let elName = std.Element("name") 
            select new
            {
                ID = std.Element("id").Value,
                Name = (elName!=null)?elName.Value:string.Empty
            };

Syntax with 'let' allows you avoid twice ask Element("name")

var stds = from std in doc.Descendants("student")
            let elName = std.Element("name") 
            select new
            {
                ID = std.Element("id").Value,
                Name = (elName!=null)?elName.Value:string.Empty
            };
滴情不沾 2024-08-29 06:09:29

这有点晚了,但可能会帮助其他人解决这个问题。我有一个相当大的对象,有 90 个属性,我试图从 XML 文件中转换成这些属性,因此为了让事情变得更容易,我创建了一些方法。

    private static object CheckElement(XElement element)
    {
        return string.IsNullOrEmpty((string)element) ? null : element.Value;
    }

    public static string CheckElementString(XElement element)
    {
        return (string)CheckElement(element);
    }

    public static Int16 CheckElementInt(XElement element)
    {
        var theResult = CheckElement(element);

        return theResult == null ? (short)-1 : Convert.ToInt16(theResult);
    }

    public static DateTime? CheckElementDateTimeNullable(XElement element)
    {
        var theResult = CheckElement(element);

        return theResult == null ? (DateTime?)null : DateTime.Parse(theResult.ToString());
    }

    public static decimal CheckElementDecimal(XElement element)
    {
        var theResult = CheckElement(element);

        return theResult == null ? 0.00M : Convert.ToDecimal(theResult);
    }

    public static bool CheckElementBoolean(XElement element, bool defaultValue)
    {
        var theResult = CheckElement(element);

        return theResult == null ? defaultValue : Convert.ToBoolean(theResult);
    }

然后就很容易使用了,如下所示:

    var stds = from std in doc.Descendants("student")
        select new
        {
            ID = std.Element("id").Value,
            Name = CheckElementString(std.Element("name"))
        };

This is a little late, but may help someone else looking through this. I had a rather large object with 90 properties I was trying to cast into from an XML file, so to make things easier I created a few methods.

    private static object CheckElement(XElement element)
    {
        return string.IsNullOrEmpty((string)element) ? null : element.Value;
    }

    public static string CheckElementString(XElement element)
    {
        return (string)CheckElement(element);
    }

    public static Int16 CheckElementInt(XElement element)
    {
        var theResult = CheckElement(element);

        return theResult == null ? (short)-1 : Convert.ToInt16(theResult);
    }

    public static DateTime? CheckElementDateTimeNullable(XElement element)
    {
        var theResult = CheckElement(element);

        return theResult == null ? (DateTime?)null : DateTime.Parse(theResult.ToString());
    }

    public static decimal CheckElementDecimal(XElement element)
    {
        var theResult = CheckElement(element);

        return theResult == null ? 0.00M : Convert.ToDecimal(theResult);
    }

    public static bool CheckElementBoolean(XElement element, bool defaultValue)
    {
        var theResult = CheckElement(element);

        return theResult == null ? defaultValue : Convert.ToBoolean(theResult);
    }

Then it was pretty easy to use like this:

    var stds = from std in doc.Descendants("student")
        select new
        {
            ID = std.Element("id").Value,
            Name = CheckElementString(std.Element("name"))
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文