簡體   English   中英

XML 反序列化 C#

[英]XML Deserialize C#

希望有人知道如何使用這個 xml 結構。 我正在嘗試反序列化某些游戲的 xml 文件。

我有以下文件:

<?xml version="1.0" ?>
<GameUI>
    <XML_Version Ver="1205" />
    <State id="100000" fontcnt="0" texturecnt="0" nodename="SCENE_COMMON_AGORA">
        <Font id="0" height="10" weight="100" name="TEST" size="100" filename="TEST" facename="TEST" />
        <Texture id="2818432626" name="effect_01.dds" height="256" width="512" filename="effect_01" />
        <Component id="610" type="15" font="0" texture="2818432626" nodename="bat_userback_start" texcoord="1.32.13.60" textformat="0" alpa="235" fontcolor="0" SourceType="0" textureNodeName="32" folderid="0" enable="1" />
        <Font id="2" height="-12" weight="500" name="TEST Regular" size="90" filename="TEST Regular_9_w500pt" facename="TEST Regular" />
        <Dialog nodename="E_DLG_AGORA_MENU" id="101000" active="0" coordinatetype="1" x="0" y="0" w="1024" h="0" ch="0" caption="1" minimize="0" de_caption="29" de_body="29" de_bodywide16_9="29" de_bodywide16_10="29" ToolVisible="1">
            <Static nodename="E_DLG_AGORA_MENU_STC_NOTICE" ScaleNormal="default" ScalePress="default" ScaleSelect="default" ScaleOver="default" ScaleDisable="default" ScaleAppear="default" ScaleDisappear="default" layerlevel="0" id="101071" coordinatetype="1" renderlevel="0" appeartime="0" disappeartime="0" appeargroup="0" disappeargroup="0" ctrlAlignType="0" alignOffset="0" fonttype="0" fontColorNormal="-269516" fontColorSelect="-16777216" fontColorOver="-16777216" fontColorDisable="-16777216" fontColorPress="-16777216" x="81" y="39" w="296" h="64" enable="1" ToolVisible="1" Element_0="4" shadow="0" text="" TextWAlign="1" TextHAlign="0" DrawUseTexture="0" TextPosX="0" TextPosY="0" display_font="216">Static</Static>
            <Static nodename="E_DLG_AGORA_MENU_STC_PLAYER_INFO_TIP" ScaleNormal="default" ScalePress="default" ScaleSelect="default" ScaleOver="default" ScaleDisable="default" ScaleAppear="default" ScaleDisappear="default" layerlevel="7" id="101072" coordinatetype="1" renderlevel="1" appeartime="0" disappeartime="0" appeargroup="0" disappeargroup="0" ctrlAlignType="0" alignOffset="0" fonttype="0" fontColorNormal="-14015975" fontColorSelect="-14015975" fontColorOver="-14015975" fontColorDisable="-14015975" fontColorPress="-14015975" x="9" y="799" w="75" h="20" enable="1" ToolVisible="1" Element_0="4" shadow="0" text="Static" TextWAlign="1" TextHAlign="4" DrawUseTexture="0" TextPosX="0" TextPosY="0" display_font="219">Static</Static>
            <Static nodename="E_DLG_AGORA_MENU_STC_POINT" ScaleNormal="default" ScalePress="default" ScaleSelect="default" ScaleOver="default" ScaleDisable="default" ScaleAppear="default" ScaleDisappear="default" layerlevel="2" id="101073" coordinatetype="1" renderlevel="0" appeartime="0" disappeartime="0" appeargroup="0" disappeargroup="0" ctrlAlignType="3" alignOffset="120" fonttype="0" fontColorNormal="-1381654" fontColorSelect="-16777216" fontColorOver="-16777216" fontColorDisable="-16777216" fontColorPress="-16777216" x="587" y="747" w="90" h="20" enable="1" ToolVisible="1" Element_0="4" shadow="0" text="0" TextWAlign="0" TextHAlign="4" DrawUseTexture="0" TextPosX="0" TextPosY="0" display_font="216">Static</Static>          
        </Dialog>
    </State>
</GameUI>

我使用https://xmltocsharp.azurewebsites.net/創建 xml 的結構。

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="XML_Version")]
    public class XML_Version {
        [XmlAttribute(AttributeName="Ver")]
        public string Ver { get; set; }
    }

    [XmlRoot(ElementName="Font")]
    public class Font {
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName="height")]
        public string Height { get; set; }
        [XmlAttribute(AttributeName="weight")]
        public string Weight { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName="size")]
        public string Size { get; set; }
        [XmlAttribute(AttributeName="filename")]
        public string Filename { get; set; }
        [XmlAttribute(AttributeName="facename")]
        public string Facename { get; set; }
    }

    [XmlRoot(ElementName="Texture")]
    public class Texture {
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName="height")]
        public string Height { get; set; }
        [XmlAttribute(AttributeName="width")]
        public string Width { get; set; }
        [XmlAttribute(AttributeName="filename")]
        public string Filename { get; set; }
    }

    [XmlRoot(ElementName="Component")]
    public class Component {
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName="type")]
        public string Type { get; set; }
        [XmlAttribute(AttributeName="font")]
        public string Font { get; set; }
        [XmlAttribute(AttributeName="texture")]
        public string Texture { get; set; }
        [XmlAttribute(AttributeName="nodename")]
        public string Nodename { get; set; }
        [XmlAttribute(AttributeName="texcoord")]
        public string Texcoord { get; set; }
        [XmlAttribute(AttributeName="textformat")]
        public string Textformat { get; set; }
        [XmlAttribute(AttributeName="alpa")]
        public string Alpa { get; set; }
        [XmlAttribute(AttributeName="fontcolor")]
        public string Fontcolor { get; set; }
        [XmlAttribute(AttributeName="SourceType")]
        public string SourceType { get; set; }
        [XmlAttribute(AttributeName="textureNodeName")]
        public string TextureNodeName { get; set; }
        [XmlAttribute(AttributeName="folderid")]
        public string Folderid { get; set; }
        [XmlAttribute(AttributeName="enable")]
        public string Enable { get; set; }
    }

    [XmlRoot(ElementName="Static")]
    public class Static {
        [XmlAttribute(AttributeName="nodename")]
        public string Nodename { get; set; }
        [XmlAttribute(AttributeName="ScaleNormal")]
        public string ScaleNormal { get; set; }
        [XmlAttribute(AttributeName="ScalePress")]
        public string ScalePress { get; set; }
        [XmlAttribute(AttributeName="ScaleSelect")]
        public string ScaleSelect { get; set; }
        [XmlAttribute(AttributeName="ScaleOver")]
        public string ScaleOver { get; set; }
        [XmlAttribute(AttributeName="ScaleDisable")]
        public string ScaleDisable { get; set; }
        [XmlAttribute(AttributeName="ScaleAppear")]
        public string ScaleAppear { get; set; }
        [XmlAttribute(AttributeName="ScaleDisappear")]
        public string ScaleDisappear { get; set; }
        [XmlAttribute(AttributeName="layerlevel")]
        public string Layerlevel { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName="coordinatetype")]
        public string Coordinatetype { get; set; }
        [XmlAttribute(AttributeName="renderlevel")]
        public string Renderlevel { get; set; }
        [XmlAttribute(AttributeName="appeartime")]
        public string Appeartime { get; set; }
        [XmlAttribute(AttributeName="disappeartime")]
        public string Disappeartime { get; set; }
        [XmlAttribute(AttributeName="appeargroup")]
        public string Appeargroup { get; set; }
        [XmlAttribute(AttributeName="disappeargroup")]
        public string Disappeargroup { get; set; }
        [XmlAttribute(AttributeName="ctrlAlignType")]
        public string CtrlAlignType { get; set; }
        [XmlAttribute(AttributeName="alignOffset")]
        public string AlignOffset { get; set; }
        [XmlAttribute(AttributeName="fonttype")]
        public string Fonttype { get; set; }
        [XmlAttribute(AttributeName="fontColorNormal")]
        public string FontColorNormal { get; set; }
        [XmlAttribute(AttributeName="fontColorSelect")]
        public string FontColorSelect { get; set; }
        [XmlAttribute(AttributeName="fontColorOver")]
        public string FontColorOver { get; set; }
        [XmlAttribute(AttributeName="fontColorDisable")]
        public string FontColorDisable { get; set; }
        [XmlAttribute(AttributeName="fontColorPress")]
        public string FontColorPress { get; set; }
        [XmlAttribute(AttributeName="x")]
        public string X { get; set; }
        [XmlAttribute(AttributeName="y")]
        public string Y { get; set; }
        [XmlAttribute(AttributeName="w")]
        public string W { get; set; }
        [XmlAttribute(AttributeName="h")]
        public string H { get; set; }
        [XmlAttribute(AttributeName="enable")]
        public string Enable { get; set; }
        [XmlAttribute(AttributeName="ToolVisible")]
        public string ToolVisible { get; set; }
        [XmlAttribute(AttributeName="Element_0")]
        public string Element_0 { get; set; }
        [XmlAttribute(AttributeName="shadow")]
        public string Shadow { get; set; }
        [XmlAttribute(AttributeName="text")]
        public string _Text { get; set; }
        [XmlText]
        public string __Text { get; set; }
        [XmlAttribute(AttributeName="TextWAlign")]
        public string TextWAlign { get; set; }
        [XmlAttribute(AttributeName="TextHAlign")]
        public string TextHAlign { get; set; }
        [XmlAttribute(AttributeName="DrawUseTexture")]
        public string DrawUseTexture { get; set; }
        [XmlAttribute(AttributeName="TextPosX")]
        public string TextPosX { get; set; }
        [XmlAttribute(AttributeName="TextPosY")]
        public string TextPosY { get; set; }
        [XmlAttribute(AttributeName="display_font")]
        public string Display_font { get; set; }
    }

    [XmlRoot(ElementName="Dialog")]
    public class Dialog {
        [XmlElement(ElementName="Static")]
        public List<Static> Static { get; set; }
        [XmlAttribute(AttributeName="nodename")]
        public string Nodename { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName="active")]
        public string Active { get; set; }
        [XmlAttribute(AttributeName="coordinatetype")]
        public string Coordinatetype { get; set; }
        [XmlAttribute(AttributeName="x")]
        public string X { get; set; }
        [XmlAttribute(AttributeName="y")]
        public string Y { get; set; }
        [XmlAttribute(AttributeName="w")]
        public string W { get; set; }
        [XmlAttribute(AttributeName="h")]
        public string H { get; set; }
        [XmlAttribute(AttributeName="ch")]
        public string Ch { get; set; }
        [XmlAttribute(AttributeName="caption")]
        public string Caption { get; set; }
        [XmlAttribute(AttributeName="minimize")]
        public string Minimize { get; set; }
        [XmlAttribute(AttributeName="de_caption")]
        public string De_caption { get; set; }
        [XmlAttribute(AttributeName="de_body")]
        public string De_body { get; set; }
        [XmlAttribute(AttributeName="de_bodywide16_9")]
        public string De_bodywide16_9 { get; set; }
        [XmlAttribute(AttributeName="de_bodywide16_10")]
        public string De_bodywide16_10 { get; set; }
        [XmlAttribute(AttributeName="ToolVisible")]
        public string ToolVisible { get; set; }
    }

    [XmlRoot(ElementName="State")]
    public class State {
        [XmlElement(ElementName="Font")]
        public List<Font> Font { get; set; }
        [XmlElement(ElementName="Texture")]
        public Texture Texture { get; set; }
        [XmlElement(ElementName="Component")]
        public Component Component { get; set; }
        [XmlElement(ElementName="Dialog")]
        public Dialog Dialog { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName="fontcnt")]
        public string Fontcnt { get; set; }
        [XmlAttribute(AttributeName="texturecnt")]
        public string Texturecnt { get; set; }
        [XmlAttribute(AttributeName="nodename")]
        public string Nodename { get; set; }
    }

    [XmlRoot(ElementName="GameUI")]
    public class GameUI {
        [XmlElement(ElementName="XML_Version")]
        public XML_Version XML_Version { get; set; }
        [XmlElement(ElementName="State")]
        public State State { get; set; }
    }

}

我的問題是,我不知道如何反序列化它,所以我可以訪問所有單個屬性等。

要對此 XML 進行反序列化,您需要使用:

using System.Xml.Serialization;

和以下代碼:

     private void btnConvert_Click(object sender, EventArgs e)
    {
        string xmlString = tbInput.Text;
        var mySerializer = new XmlSerializer(typeof(GameUI));

        GameUI gameUI;
        using (StringReader reader = new StringReader(xmlString))
        {
            gameUI = (GameUI)mySerializer.Deserialize(reader);
            "gameUI.State.Id.ToString() - " +gameUI.State.Id.ToString();
        }
    }

結果:

在此處輸入圖像描述

您可以使用本網站的課程:

XML 至 C# Class

您需要 xml 的根 class

public class Root
{
  [JsonProperty("?xml")]
  public XML_Version Xml { get; set; }
  public GameUI GameUI { get; set; }
}

所以使用 Newtonsoft 你可以做這樣的事情

using Newtonsoft.Json;

public void Test(string xml)
{
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(xml);
  string jsonText = JsonConvert.SerializeXmlNode(doc);
  var xMLObject = JsonConvert.DeserializeObject<Root>(jsonText);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM