首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取元素列表的Linq异常

读取元素列表的Linq异常
EN

Stack Overflow用户
提问于 2016-11-11 09:42:34
回答 2查看 44关注 0票数 1

我试图从我的Xml文件中读取一个文本节点列表。当我试图读取列表中的一个文本元素的子节点时,我会得到NullReference异常,尽管XDocument节点似乎已被填充。NullReference异常将抛出在foreach (XElement textElement in textElements)行上,这意味着list textElements的计数大于1,因此不应该为null。这就是调试器愿意做的事情。

我是以错误的方式读取Xml,还是不允许/不应该以现在的方式构建XElement列表?

这是我的Xml文件

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Book SYSTEM "Book.dtd"[]>
<album version="0.2" settingsVersion="1">
  <size />
  <cover />
  <inner>
    <page>
      <pagenumber>1</pagenumber>
      <image>1.jpg</image>
      <text>
        <value>Test </value>
        <font>arial</font>
        <size>18pt</size>
        <style>normal</style>
        <weight>normal</weight>
        <color>#77DD44</color>
        <rotation>0</rotation>
        <alignment>start</alignment>
        <position>
          <x>50</x>
          <y>50</y>
        </position>
      </text>
      <text>
          <value>Test 2 </value>
          <font>arial</font>
          <size>18pt</size>
          <style>normal</style>
          <weight>normal</weight>
          <color>#77DD44</color>
          <rotation>0</rotation>
          <alignment>start</alignment>
          <position>
             <x>50</x>
             <y>50</y>
          </position>
       </text>
    </page>
  </inner>
</album>

元素inner可以容纳多个page元素,page可以容纳多个text元素。

我使用以下方法读取Xml。

代码语言:javascript
运行
复制
List<XElement> pageElements = doc.Root.Element("inner").Elements("page").ToList();
foreach (XElement pageElement in pageElements)
{
    string pageNumberString = pageElement.Element("pagenumber").Value;
    int pageNumberValue = Convert.ToInt32(pageNumberString);
    string fileNameValue = pageElement.Element("image").Value;
    // Verify if the currently looped page is the same as the one selected by the user.
    if (pageNumberValue == pageNumber)
    {
        // Get all text nodes from the found page.
        List<XElement> textElements = pageElement.Elements("text").ToList();
        // If no text nodes found return the page with an empty TextInfo array.
        if (textElements.Count == 0)
        {
            PageInfo pageInfoNoText = new PageInfo { PageNumber = pageNumberValue, FileName = fileNameValue, Text = new TextInfo[0] };
            Logger.log("PageInfo found for collection {0}. Info {1}", collectionId, pageInfoNoText);
            return pageInfoNoText;
        }
        // If text nodes are found build a list of TextInfo objects and build a new PageInfo object.
        else
        {
            // All text elements in the XML under the found page.
            List<TextInfo> textInfoList = new List<TextInfo>();
            TextInfo[] textArray = new TextInfo[0];

            #region  Load all required text data from the XML file and build the textList.
            foreach (XElement textElement in textElements)
            {
                string textValue = textElement.Element("value").Value;

                string fontValue = textElement.Element("font").Value;

                string fontSizeValue = textElement.Element("size").Value;

                string styleValue = textElement.Element("style").Value;

                string weightValue = textElement.Element("weight").Value;

                string colorValue = textElement.Element("color").Value;

                string rotationString = textElement.Element("rotation").Value;
                int rotationValue = Convert.ToInt32(rotationString);

                string alignmentValue = textElement.Element("alignment").Value;

                string positionXString = textElement.Element("x").Value;
                int positionXValue = Convert.ToInt32(positionXString);

                string positionYString = textElement.Element("y").Value;
                int positionYValue = Convert.ToInt32(positionYString);

                // Build Info objects.
                PositionInfo tempPositionInfo = new PositionInfo
                {
                    X = positionXValue,
                    Y = positionYValue
                };

                TextInfo tempTextInfo = new TextInfo
                {
                    Value = textValue,
                    Font = fontValue,
                    Size = fontSizeValue,
                    Style = styleValue,
                    Weight = weightValue,
                    Color = colorValue,
                    Rotation = rotationValue,
                    Alignment = alignmentValue,
                    Position = tempPositionInfo
                };

                textInfoList.Add(tempTextInfo);

            }
            textArray = textInfoList.ToArray();
            #endregion

            PageInfo pageInfo = new PageInfo
            {
                PageNumber = pageNumberValue,
                FileName = fileNameValue,
                Text = textArray
            };
            Logger.log("PageInfo found for collection {0}. Info: {1}", collectionId, pageInfo);
            return pageInfo;

        }
    }
}

任何帮助/洞察力都将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-11 10:01:03

您的代码很好,但是当您获得x和y值时,您需要获得position元素的x和y值,如下所示

代码语言:javascript
运行
复制
string positionXString = (string)textElement.Element("position").Element("x").Value;
int positionXValue = Convert.ToInt32(positionXString);
string positionYString = (string)textElement.Element("position").Element("y").Value;
int positionYValue = Convert.ToInt32(positionYString);
票数 1
EN

Stack Overflow用户

发布于 2016-11-11 10:00:56

文本元素不包含名为x的元素,因此试图访问NullReferenceException中x的值:

代码语言:javascript
运行
复制
string positionXString = textElement.Element("x").Value;
// textElement.Element("x") is null, "x" is in textElement.Element("position")
int positionXValue = Convert.ToInt32(positionXString);

您可能会更好地使用Descendants (Descendants("x").FirstOrDefault()?.Value),而不是为此使用元素,但总的来说,您可能希望对其进行完全重构。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40545120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档