简体   繁体   English

C# 获取 XmlNode 的值 object

[英]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 的引用

  • In VS menu, click Project在 VS 菜单中,单击项目
  • Select Add Reference Select添加参考
  • Check System.Xml.Serialization检查System.Xml.Serialization
  • Click OK点击确定

Add using statement : using System.Xml.Serialization;添加 using 语句using System.Xml.Serialization;

Create class : requisitionsRequisition创建 class : requisitionsRequisition

  • In VS menu, click Project在 VS 菜单中,单击项目
  • Select Add Class (name: requisitionsRequisition) Select添加 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

  • In VS menu, click Project在 VS 菜单中,单击项目
  • Select Add Class (name: HelperXml) Select添加 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM