我正在将一些XML导入C#,并希望能够以我认为是嵌套结构的形式从XML访问数据。 (我可能错了!)
我在XML中的内容如下:
<hardwareSettings initial="true>
<cameraSettings width="1024" height="768" depth="8" />
<tiltSettings theta="35" rho="90">
</hardwareSettings>
我可以导入每个设置,所以我将它们全部用于单独的整数,但我希望能够以表格形式访问它
int x=hardwaresettings.camerasettings.width;
int rho=hardwaresettings.tiltsettings.rho;
我已尝试在结构体内进行各种结构排列,但我似乎无法投射包含相应子项的新对象(hardwaresettings)(camerasettings.width&amp; tiltsettings.rho)。
对不起,如果我没有使用正确的术语......我在这里读圈子了!
答案 0 :(得分:1)
public class TiltSettings
{
public TiltSettings(XElement element)
{
this.Theta = Convert.ToInt32(element.Attribute("theta").Value);
this.Rho = Convert.ToInt32(element.Attribute("rho").Value);
}
public int Theta {get; set;}
public int Rho { get; set; }
}
public class CameraSettings
{
public CameraSettings(XElement element)
{
this.Height = Convert.ToInt32(element.Attribute("height").Value);
this.Width = Convert.ToInt32(element.Attribute("width").Value);
this.Depth = Convert.ToInt32(element.Attribute("depth").Value);
}
public int Width {get; set;}
public int Height {get; set;}
public int Depth { get; set; }
}
public class HardwareSettings
{
public HardwareSettings(string xml)
{
var xDoc = XDocument.Parse(xml);
this.IsInitial = xDoc.Root.Attribute("initial").Value == "true";
this.Camera = new CameraSettings(xDoc.Root.Element("cameraSettings"));
this.Tilt = new TiltSettings(xDoc.Root.Element("tiltSettings"));
}
public CameraSettings Camera {get; set;}
public TiltSettings Tilt {get; set;}
public bool IsInitial {get; set;}
}
答案 1 :(得分:0)
您可以通过在XDocument类上编写自定义包装器来实现此类功能。
答案 2 :(得分:0)
class HardwareSettings
{
public TiltSettings TiltSettings { get; set; }
public CameraSetting CameraSetting { get; set; }
}
struct CameraSetting
{
public int Width { get; set; }
public int Height { get; set; }
public int Depth { get; set; }
}
struct TiltSettings
{
public int Theta { get; set; }
public int Rho { get; set; }
}
修改强>
以"properties"
HardwareSettings.CameraSetting.Depth
XElement elements = XElement.Load("XMLFile.xml");
List<HardwareSettings> hardwareSettingsList =
(from e in elements.Descendants("cameraSettings")
select new HardwareSettings
{
CameraSetting = new CameraSetting()
{
Depth = Convert.ToInt32(e.Attribute("width").Value),
Height = Convert.ToInt32(e.Attribute("width").Value),
Width = Convert.ToInt32(e.Attribute("width").Value)
},
TiltSettings = new TiltSettings()
{ } // your logic for rest of the structs
}).ToList<HardwareSettings>();
int depth = 0;
foreach (HardwareSettings hardwareSetting in hardwareSettingsList)
{
depth = hardwareSetting.CameraSetting.Depth;
}
答案 3 :(得分:0)
考虑创建设置类并使用内置或自定义SettingsProvider
。
您将创建该类并继承ApplicationSettingsBase
有很多关于此的文档,但这里是一个非常快速的snippit。
internal sealed partial class TestSettings : ApplicationSettingsBase
{
private static TestSettings settings = ((TestSettings)(ApplicationSettingsBase.Synchronized(new TestSettings())));
public static TestSettings Settings { get { return settings; } }
[UserScopedSetting()]
public CameraSettingsClass CameraSettings
{
get
{
try
{
if(this["CameraSettings"] == null) return (CameraSettingsClass)(new CameraSettingsClass());
else return (CameraSettingsClass)this["CameraSettings"];
}
catch(Exception error) {throw new Exception("Problem with accessing CameraSettings");}
}
set { this["CameraSettings"] = value; }
}
}
public class CameraSettingsClass
{
[XmlAttribute]
public int Width {get;set;}
[XmlAttribute]
public int Height {get;set;}
[XmlAttribute]
public int Depth {get;set;}
public CameraSettingsClass()
{
this.Width = 1024;
this.Height = 768;
this.Depth = 8;
}
}
//Access of the settings as follows
TestSettings.Settings.CameraSettings
//or
TestSettings.Settings["CameraSettings"]
<TestProject.TestSettings>
<setting name="CameraSettings" serializeAs="Xml">
<value>
<CameraSettingsClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" Width="512" Height="768"
Depth="8" />
</value>
</setting>
</TestProject.TestSettings>
您可以在此处查看有关使用Different SettingsProviders的详细信息的更多信息 http://msdn.microsoft.com/en-us/library/ms171565(VS.80).aspx
使用设置提供程序和ApplicationSettingsBase将为您提供.Save()。Reload()。Upgrade()等功能。