【发布时间】:2018-11-02 23:16:51
【问题描述】:
我需要一些帮助来选择所有<encoding> 节点并将它们添加到List<Core.Encoding>。我得到了高度和宽度的值,但是当我取消注释 VideoCodec、AudioCodec 和 ContainerType 时,我得到了错误
对象引用未设置为对象的实例。
有人有什么想法吗?
<media>
<id>123456</id>
<media_type>Video</media_type>
<encodings>
<encoding>
<height>270</height>
<width>404</width>
<video_codec>H264</video_codec>
<audio_codec>Aac</audio_codec>
<container_type>Mp4</container_type>
</encoding>
<encoding>
<height>270</height>
<width>404</width>
<video_codec>H264</video_codec>
<audio_codec>Aac</audio_codec>
<container_type>Mp4</container_type>
</encoding>
</encodings>
</media>
这是我目前所拥有的:
[Serializable]
public class Encoding
{
public string Height { get; set; }
public string Width { get; set; }
public string VideoCodec { get; set; }
public string AudioCodec { get; set; }
public string ContainerType { get; set; }
}
private List<Core.Encoding> SomeMethod(string authenticatedURL)
{
XElement xml = XElement.Load(authenticatedURL);
list.AddRange((from encoding in xml.DescendantsAndSelf("encoding")
select new Core.Encoding
{
Height = encoding.Element("height").Value,
Width = encoding.Element("width").Value,
VideoCodec = encoding.Element("video_codec").Value,
AudioCodec = encoding.Element("audio_codec").Value,
ContainerType = encoding.Element("container_type").Value
}));
}
【问题讨论】:
-
@T.S.你确定吗?您似乎没有意识到构造函数后面可能跟有对象初始化器
{},如果是,()就成为可选的。 docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… -
@spender 哎呀。你是对的。看,我们不再做这么多。因为原子初始化期间的任何错误都很难调试。如果你不使用它,你就会失去它。我现在什至记得编辑把括号去掉了,因为它们不是必需的
-
未设置的对象引用可以是任何东西 -
list,authenticateUrl.. -
好的,我明白了。我错过了您可以评论该元素的观点。我更新了我的答案以解决这种情况