C# 如何使用C嵌套结构访问数据树
我正在将一些XML导入C,希望能够以我认为是嵌套结构的形式从XML中访问数据。我可能错了 我的XML中包含的内容如下所示:C# 如何使用C嵌套结构访问数据树,c#,data-structures,C#,Data Structures,我正在将一些XML导入C,希望能够以我认为是嵌套结构的形式从XML中访问数据。我可能错了 我的XML中包含的内容如下所示: <hardwareSettings initial="true> <cameraSettings width="1024" height="768" depth="8" /> <tiltSettings theta="35" rho="90"> </hardwareSettings> 我尝试了结构中的各种结构
<hardwareSettings initial="true>
<cameraSettings width="1024" height="768" depth="8" />
<tiltSettings theta="35" rho="90">
</hardwareSettings>
我尝试了结构中的各种结构安排,但似乎无法强制转换包含相应子项camerasettings.width&tiltsettings.rho的新对象硬件设置
对不起,如果我没有使用正确的术语。。。我在这里读我自己 您可以通过在XDocument类上编写自定义包装来实现此类功能。您可以通过在XDocument类上编写自定义包装来实现此类功能。如果我没有弄错您的问题,可能类似的功能也可以实现
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;}
}
编辑:
访问类属性如:HardwareSettings.CameraSetting.Depth
如果我没弄错你的问题,也许类似的方法也行 编辑: 访问类属性如:HardwareSettings.CameraSetting.Depth
考虑创建一个设置类并使用内置或自定义设置Provider 您将创建该类并继承ApplicationSettingsBase 关于这一点有很多文档,但这里有一个非常简单的例子 设置类 节点的XML输出 您可以在此处查看有关使用不同设置Providers的详细信息
使用设置提供程序和ApplicationSettingsBase将为您提供.Save.Reload.Upgrade等。。功能。考虑创建一个设置类,并使用内置或自定义设置Provider 您将创建该类并继承ApplicationSettingsBase 关于这一点有很多文档,但这里有一个非常简单的例子 设置类 节点的XML输出 您可以在此处查看有关使用不同设置Providers的详细信息
使用设置提供程序和ApplicationSettingsBase将为您提供.Save.Reload.Upgrade等。。功能性。这正是我半小时前尝试过的。。。但是,当我铸造一个新的硬件设置对象时,我不能引用子对象,即铸造硬件设置HS=新硬件设置sxml;HS.CameraSettings.Width不起作用…我编辑了我的答案,现在我对您要查找的内容有了更好的了解。另外,当你说“铸造一个新对象”时,我想你的意思是“实例化一个新对象”=这正是我半小时前试过的。。。但是,当我铸造一个新的硬件设置对象时,我不能引用子对象,即铸造硬件设置HS=新硬件设置sxml;HS.CameraSettings.Width不起作用…我编辑了我的答案,现在我对您要查找的内容有了更好的了解。另外,当你说“铸造一个新对象”时,我想你的意思是“实例化一个新对象”=这是那里90%的道路。。。但我希望CameraSetting是硬件设置的子项,这样我就可以像int depth=hardwareSetting.CameraSetting.depth一样访问它;另外,如果我可以问的话,这里你以一种非常聪明的方式投下了一个新的摄影师,但我不知道如何称呼它。。。如果我想恢复深度值,我不能调用results.CameraSetting。。。我该怎么做?这是90%的方法。。。但我希望CameraSetting是硬件设置的子项,这样我就可以像int depth=hardwareSetting.CameraSetting.depth一样访问它;另外,如果我可以问的话,这里你以一种非常聪明的方式投下了一个新的摄影师,但我不知道如何称呼它。。。如果我想恢复深度值,我不能调用results.CameraSetting。。。我该怎么做?
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;}
}
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; }
}
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;
}
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>