2013-02-14 55 views
0

*切換到分離。C#使用內部具有變量名稱的字符串設置變量

摘要:我將所有變量預先定義爲null/0.我想使用來自和XML文檔的數據來設置它們。該文檔包含與變量完全相同的名稱。我不想使用其他一些ifs,所以我正在嘗試根據從XML文檔中提取的名稱來完成此操作。

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Reflection; 
using System.Text; 
using System.Xml; 
using System.IO; 

public class ShipAttributes 
{ 
    // Model Information 
    string name; 
    string modelName; 
    string firingPosition; 
    string cameraPosition; 
    string cargoPosition; 
    Vector3 cameraDistance; 

    // Rotation 
    float yawSpeed; 
    float pitchSpeed; 
    float rollSpeed; 

    // Speed 
    float maxSpeed; 
    float cruiseSpeed; 
    float drag; 
    float maxAcceleration; 
    float maxDeceleration; 

    // Physics Properties 
    float mass; 

    // Collection Capacity 
    float cargoSpace; 

    // Combat [Defense] 
    float structureHealth; 
    float armorHealth; 
    float shieldHealth; 
    float shieldRadius; 

    // Combat [Assault] 
    float missileRechargeTime; 
    float fireRate; 

    // Currency Related 
    float cost; 
    float repairMultiplier; 

void PopulateShipList() 
{ 
    if (shipList != null) 
     return; 

    string filepath = Application.dataPath + "/Resources/shipsdata.xml"; 

    XmlRootAttribute xml_Root = new XmlRootAttribute(); 
    xml_Root.ElementName = "SHIPS"; 
    xml_Root.IsNullable = true; 
    //using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read)) 
    //{ 
     StringReader stringReader = new StringReader(filepath); 
     stringReader.Read(); 
     XmlReader xRdr = XmlReader.Create(stringReader); 
     XmlSerializer xml_s = new XmlSerializer(typeof(List<ShipAttributes>), xml_Root); 
     shipList= (List<ShipAttributes>)xml_s.Deserialize(xRdr); 
    //} 
} 
public ShipAttributes LoadShip(string inName) 
{ 
    PopulateShipList(); 
    foreach (ShipAttributes att in shipList) 
    { 
     if (att.name == inName) 
     { 
      att.shipList = shipList; 
      return att; 
     } 
    } 

    return null; 
} 

*說明XML文件中的變量與類中的變量具有完全相同的格式和名稱。 Class中的maxSpeed是XML文件中的maxSpeed。

我的XML看起來是這樣的 -

<?xml version="1.0" encoding="UTF-8 without BOM" ?> 

    <SHIPS> 
     <SHIP> 
      <name>Default</name> 
      <id>0</id> 
      <modelName>Feisar_Ship</modelName> 
      <firingPosition>null</firingPosition> 
      <cameraPosition>null</cameraPosition> 
      <cargoPosition>null</cargoPosition> 
      <cameraDistance>null</cameraDistance> 
      <yawSpeed>2000.0</yawSpeed> 
      <pitchSpeed>3000.0</pitchSpeed> 
      <rollSpeed>10000.15</rollSpeed> 
      <maxSpeed>200000.0</maxSpeed> 
      <cruiseSpeed>100000.0</cruiseSpeed> 
      <drag>0.0</drag> 
      <maxAcceleration>null</maxAcceleration> 
      <maxDeceleration>null</maxDeceleration> 
      <mass>5000.0</mass> 
      <cargoSpace>150.0</cargoSpace> 
      <structureHealth>100.0</structureHealth> 
      <armorHealth>25.0</armorHealth> 
      <shieldHealth>25.0</shieldHealth> 
      <shieldRadius>30.0</shieldRadius> 
      <missileRechargeTime>2.0</missileRechargeTime> 
      <fireRate>0.5f</fireRate> 
      <cost>0</cost> 
      <repairMultiplier>1.0</repairMultiplier> 
     </SHIP> 
    </SHIPS 

>

是的是的,我知道... Feisar!現在只需從turbosquid放置持有者即可。

+1

我不明白你的第三和第四步......?你是這些變量的一部分嗎?而且,這個類是否可以直接從XML文檔反序列化呢?最後:實際上並不確定你在問什麼:) – baldric 2013-02-14 06:24:20

+0

你的類的示例是有用的(假設「變量」是指類的屬性/字段,而不是某種方法的局部變量)。 – 2013-02-14 06:25:49

+0

從我正在做的事情更新代碼。我試圖設置變量而不用說speed = attribute.innertext。 我想說[Psuedo] attribute.name.thisIsNowTheVariable =屬性。的innerText; 屬性是一個XML節點。 – 2013-02-14 06:32:36

回答

3

創建一個類來保存您正在閱讀的值並使用XML序列化。

Microsoft Tutorial

Tech Pro Tutorial

MSDN: XmlSerializer

編輯:

如果XML是完全一樣的,你貼類代碼,下面應該讓你得到一個類ShipAttributes

ShipAttributes attributes = null; 
string filepath = "/path/to/xml"; 

using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { 
    var xml_s = new XmlSerializer(typeof(ShipAttributes)); 

    attributes = (ShipAttributes)xml_s.Deserialize(stream); 
} 

編輯2:多個船舶

您的評論,你說的那個文件包含多個ShipAttribute描述。處理這件事的方法是與上面相同,但文件反序列化爲類型List<ShipAttribute>如下:

List<ShipAttributes> ships = null; 
string filepath = "/path/to/xml"; 

using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { 
    var xml_s = new XmlSerializer(typeof(List<ShipAttributes>)); 

    ships= (List<ShipAttributes>)xml_s.Deserialize(stream); 
} 

一旦你做到了這一點,你把所有的船在內存中,可以挑一個出來列表中,你需要使用LINQ等等

+0

我沒遇到過這個,還沒有嘗試過序列化呢。這對我來說是新的。如果另一種方式行不通,明天就會解決。 – 2013-02-14 06:45:16

+0

編輯: 有趣的,所以如果我把變量的順序放在類裏面的順序是相同的,那麼這會設置類中的所有屬性,因爲它被讀入? – 2013-02-14 21:01:31

+0

Errr,唯一的問題是,我將在XML中存儲多艘船,所以我必須能夠跳過它們,直到找到我需要的船。反序列化似乎不允許這種類型的工作。 – 2013-02-14 21:15:03

0

假設你的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<Variables>  
    <VariableName1>1</VariableName1> 
    <VariableName2>test</VariableName2> 
    <VariableName3>test2</VariableName3>  
</Variables> 



var data = XDocument.Load("YourXML.xml"); 
var xmldata = from x in data.Descendants("Variables")       
         select x; 

notes.Select(_BuildPropertyFromElement); 



private Yourclassname _BuildNoteFromElement(XElement element) 
    { 
     var type = typeof(**Yourclassname**); 
     var Details = new Yourclassname(); 

     foreach (var propInfo in type.GetProperties()) 
     { 
     var rawValue = element.Element(propInfo.Name).Value; 
     var convertedValue = propInfo.PropertyType == typeof(DateTime) ?   (object)Convert.ToDateTime(rawValue) : rawValue; 
      propInfo.SetValue(Details , convertedValue, null); 
       } 

       return Details ; 
      } 

YOURCLASSNAME是這將有所有你想要的屬性本身的類名t到

+0

正確的方法將序列化它,但我已經發布了一種方式,你正在嘗試 – Chief 2013-02-14 06:38:51

+0

我會給這個看看當我的大腦沒有入睡。我明天會就此回覆你。 – 2013-02-14 06:44:35