[英]C# Get value of XmlNode object
XmlNode
objects are supposed to have a Value
property, as explained here , but I'm having trouble accessing it. XmlNode
对象应该具有Value
属性,如此处所述,但我无法访问它。 I have serialized an XML string into this class:我已将 XML 字符串序列化到此 class 中:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class requisitionsRequisition
{
public object start_date { get; set; }
public object end_date { get; set; }
public object title { get; set; }
}
Here's where I do the work:这是我工作的地方:
using (var reader = new StringReader(content))
{
var deserializer = new XmlSerializer(typeof(requisitionsRequisition), new XmlRootAttribute("requisitionsRequisition"));
var r = (requisitionsRequisition)deserializer.Deserialize(reader);
return View("Index", r);
}
Here's a screenshot of what I see in my debugger.这是我在调试器中看到的屏幕截图。 I can't access value, it doesn't exist.
我无法访问价值,它不存在。 My types are wrong somehow.
我的类型在某种程度上是错误的。
When I try to access the Title property, it tells me the type is {System.Xml.XmlNode[1]}
but when I try to use the Value
property, it tells me it doesn't exist.当我尝试访问 Title 属性时,它告诉我类型是
{System.Xml.XmlNode[1]}
但是当我尝试使用Value
属性时,它告诉我它不存在。
Sorry, I know you guys on here hate screenshots:)对不起,我知道你们这里讨厌截图:)
Try the following:尝试以下操作:
VS 2017 / VS 2019 VS 2017 / VS 2019
Add Reference to System.Xml.Serialization添加对 System.Xml.Serialization 的引用
Add using statement : using System.Xml.Serialization;
添加 using 语句:
using System.Xml.Serialization;
Create class : requisitionsRequisition创建 class : requisitionsRequisition
Add using statement : using System.Xml.Serialization;
添加 using 语句:
using System.Xml.Serialization;
requisitionsRequisition.cs申请Requisition.cs
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class requisitionsRequisition
{
[XmlElementAttribute(ElementName = "start_date")]
public object start_date { get; set; }
[XmlElementAttribute(ElementName = "end_date")]
public object end_date { get; set; }
[XmlElementAttribute(ElementName = "title")]
public object title { get; set; }
}
Create class : HelperXml创建 class :HelperXml
Add using statement : using System.Xml.Serialization;
添加 using 语句:
using System.Xml.Serialization;
HelperXml.cs HelperXml.cs
public static T DeserializeXMLFileToObject<T>(string xmlFilename)
{
T rObject = default(T);
try
{
if (string.IsNullOrEmpty(xmlFilename))
{
return default(T);
}
using (System.IO.StreamReader xmlStream = new System.IO.StreamReader(xmlFilename))
{
//create new instance
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
//deserialize - read XML file
rObject = (T)serializer.Deserialize(xmlStream);
}
}//try
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}//catch
return rObject;
}
public static void SerializeObjectToXMLFile(object obj, string xmlFilename)
{
try
{
if (string.IsNullOrEmpty(xmlFilename))
{
return;
}//if
using (System.IO.TextWriter xmlStream = new System.IO.StreamWriter(xmlFilename))
{
//create new instance
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
//serialize - write XML file
serializer.Serialize(xmlStream, obj);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
Usage : (Serialize) - Write file用法:(序列化) - 写入文件
string filename = @"C:\Temp\XmlFile1.xml";
requisitionsRequisition r1 = new requisitionsRequisition();
r1.title = "This is my title";
//write to file
HelperXml.SerializeObjectToXMLFile(r1, filename);
Usage : (Deserialize) - Read file用法:(反序列化) - 读取文件
string filename = @"C:\Temp\XmlFile1.xml";
//read from file
requisitionsRequisition r2 = HelperXml.DeserializeXMLFileToObject<requisitionsRequisition>(filename);
System.Diagnostics.Debug.WriteLine("title: " + r2.title.ToString());
As Alexei Levenkov pointed out in the comments.正如阿列克谢列文科夫在评论中指出的那样。 The type of the requisitionsRequisition properties was of a generic
Object
. requisitionsRequisition 属性的类型是通用的
Object
。
I couldn't access the Value property because the type was no longer XmlNode
.我无法访问 Value 属性,因为类型不再是
XmlNode
。 I simply cast back to XmlNode and I can access the values easily now.我只是简单地转换回 XmlNode,现在可以轻松访问这些值。
I changed this:我改变了这个:
var title = r.requisitions[0].title
to this:对此:
var title = (XmlNode[])r.requisition[0].title;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.