如何使用C#读取XML文件

时间:2019-04-06 08:03:29

标签: c# xml serialization

我想使用C#(统一项目)读取XML文件以显示一些内容。 XML文件由Talend Studio中的TALEND作业提供。

我已经将XML文件转换为C#类,现在正在尝试读取此XML(存储在文件中)来管理内容。

目前,我能够读取一些属性,但最有趣的标记是nul。反序列化过程中没有错误。

private const string filename = "/Applications/TalendStudio-7.1.1/studio/workspace/LOCAL_PROJECT/process/job1_0.1.item";
void Start()
{  
    // Create an instance of the XmlSerializer.
    XmlSerializer serializer = new XmlSerializer(typeof(Xml2CSharp.ProcessType));

    // Declare an object variable of the type to be deserialized.
    Xml2CSharp.ProcessType i;

    using (Stream reader = new FileStream(filename, FileMode.Open))
    {
        // Call the Deserialize method to restore the object's state.
        i = (Xml2CSharp.ProcessType)serializer.Deserialize(reader); 
        Debug.Log(i);         
    }
}

XML文件开头:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<talendfile:ProcessType 
xmlns:talendfile="TalendFile.xsd" xmlns:TalendMapper="http://www.talend.org/mapper" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
defaultContext="Default" jobType="Standard" xmi:version="2.0">
    <context confirmationNeeded="false" name="Default">
      <contextParameter comment="" name="max_rows" prompt="max_rows?" promptNeeded="false" type="id_Integer" value="10"/>
    </context>
    <context confirmationNeeded="false" name="prod">
      <contextParameter comment="" name="max_rows" prompt="max_rows?" promptNeeded="false" type="id_Integer" value="30"/>
    </context>
    <context confirmationNeeded="false" name="qa">
      <contextParameter comment="" name="max_rows" prompt="max_rows?" promptNeeded="false" type="id_Integer" value="20"/>
    </context>
<parameters>
   <elementParameter field="TEXT" name="SCREEN_OFFSET_X" show="false" value="0"/>
   <elementParameter field="TEXT" name="SCREEN_OFFSET_Y" show="false" value="0"/>
   <elementParameter field="TEXT" name="REPOSITORY_CONNECTION_ID" show="false" value=""/>

等...

“上下文”和“参数”为空。谢谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

使用xml序列化:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            XmlSerializer serializer = new XmlSerializer(typeof(ProcessType));

            ProcessType processType = (ProcessType)serializer.Deserialize(reader);


        }
    }
    [XmlRoot(ElementName = "ProcessType", Namespace = "TalendFile.xsd")] 
    public class ProcessType
    {
        [XmlAttribute("defaultContext")]
        public string defaultContext { get; set; }

        [XmlAttribute("jobType")]
        public string jobType { get; set; }

        [XmlElement("context", Namespace = "")]
        public List<Context> context { get; set; }

        [XmlArray("parameters", Namespace = "")]
        [XmlArrayItem("elementParameter", Namespace = "")]
        public List<Parameters> parameters { get; set; } 

   }
   [XmlRoot("context", Namespace = "")]
   public class Context
   {
       [XmlAttribute("confirmationNeeded")]
       public string confirmationNeeded { get; set; }

       [XmlAttribute("name")]
       public string name { get; set; }

       [XmlElement("contextParameter", Namespace = "")]
       public ContextParameter contextParameter { get; set; }
   }
   [XmlRoot("contextParameter", Namespace = "")]
   public class ContextParameter
   {
       [XmlAttribute("comment")]
       public string comment { get; set; }

       [XmlAttribute("name")]
       public string name { get; set; }

       [XmlAttribute("prompt")]
       public string prompt { get; set; }

       [XmlAttribute("promptNeeded")]
       public string promptNeeded { get; set; }

       [XmlAttribute("type")]
       public string type { get; set; }

       [XmlAttribute("value")]
       public int value { get; set; }
   }
    [XmlRoot("parameters", Namespace = "")]
   public class Parameters
   {
       [XmlAttribute("field")]
       public string field { get; set; }

       [XmlAttribute("name")]
       public string name { get; set; }

       [XmlAttribute("show", Namespace = "")]
       public string show { get; set; }

       private int? _value { get; set; }
       [XmlAttribute("value")]
       public string value {
           get { return _value.ToString(); }
           set { _value = value == string.Empty ? (int?)null : int.Parse(value); }
       }
   }
}