Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML导入类属性_C#_Xml_Winforms_Import - Fatal编程技术网

C# 将XML导入类属性

C# 将XML导入类属性,c#,xml,winforms,import,C#,Xml,Winforms,Import,我有以下格式的XML: <?xml version="1.0" standalone="yes"?> <DocumentElement> <Session> <bIsImages>False</bIsImages> <bIsPlayMedia>False</bIsPlayMedia> <bIsSubject>False</bIsSubject> <bIsVideo>False&

我有以下格式的XML:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Session>
<bIsImages>False</bIsImages>
<bIsPlayMedia>False</bIsPlayMedia>
<bIsSubject>False</bIsSubject>
<bIsVideo>False</bIsVideo>
<dtCreDate>2012-07-23</dtCreDate>
<dtSes_Date1>2001-01-01</dtSes_Date1>
<dtSes_Date2>2001-01-01</dtSes_Date2>
<dtSes_Date3>2001-01-01</dtSes_Date3>
<nClient_ID>32</nClient_ID>
<nDelay>32</nDelay>
<nImage_ID>32</nImage_ID>
<nOperator_ID>32</nOperator_ID>
<nSession_ID>32</nSession_ID>
<nVitality>32</nVitality>
<strDescr>qi stagnatie abq</strDescr>
<strMediaPath></strMediaPath>
<strName>qi stagnatie abq</strName>
<strPrimCause></strPrimCause>
<strSubjectPath>IDF_Eric duBosc.JPG</strSubjectPath>
</Session>
<SessionProgramData>
</SessionProgramData><SessionSubProgramData>
</SessionSubProgramData><SessionTuningData>
<SessionTuning>
    <SessionTuning_ID>332</SessionTuning_ID>
    <Session_ID>33</Session_ID>
    <Tuning>Brjesh</Tuning>
    <TuningDescr>Brijesh Desc</TuningDescr>
    <TuningIsNegative>false</TuningIsNegative>
    <TuningAddInfo>33</TuningAddInfo>
    <Amp>4.8</Amp>
    <Amp2 />
    <Amp3 />
    <Amp4 />
    <Amp5 />
    <Amp6 />
    <TunFreq>Brjesh</TunFreq>
    <TunFreq2 />
    <TunFreq3 />
    <TunFreq4 />
    <TunFreq5 />
    <TunFreq6 />
    <Revision2>false</Revision2>
    <Revision3>false</Revision3>
    <Revision4>false</Revision4>
    <Revision5>false</Revision5>
    <Revision6>false</Revision6>
    <AlreadyBalanced>false</AlreadyBalanced>
    <ImagePath>E:\Live Projects with Latest Source Code\SE-5\SE-5-Latest-04March11-Multilanguage-Chinese\SE-5\bin\Release\Images\</ImagePath>
    <Amp7 />
    <TunFreq7 />
    <Revision7>0</Revision7>
    <Description>Brijesh Note</Description>
    <TunRevDate>2013-02-20T18:08:48+05:30</TunRevDate>
    <TunRevDate2>2013-02-20T18:08:48+05:30</TunRevDate2>
    <TunRevDate3>2013-02-20T18:08:48+05:30</TunRevDate3>
    <TunRevDate4>2013-02-20T18:08:48+05:30</TunRevDate4>
    <TunRevDate5>2013-02-20T18:08:48+05:30</TunRevDate5>
    <TunRevDate6>2013-02-20T18:08:48+05:30</TunRevDate6>
    <TunRevDate7>2013-02-20T18:08:48+05:30</TunRevDate7>
    <Tuning_ID>20568</Tuning_ID>
</SessionTuning>
....So on
<SessionTuning>
    .....
    .....
</SessionTuning>

</SessionTuningData>
<Client>
<nClient_ID>32</nClient_ID>
<strAddress></strAddress>
<strCity></strCity>
<strCountry></strCountry>
<strFirstName>Eric</strFirstName>
<strImage>IDF_Eric duBosc.JPG</strImage>
<strLastName>Bosc</strLastName>
<strMI>du</strMI>
<strNote>ikke</strNote>
<strPhoneNum></strPhoneNum>
<strPostalCode></strPostalCode>
<strState></strState>
<strWorkPhone></strWorkPhone>
</Client>
<SE-5 />
</DocumentElement>
但是,如果我知道最后一个属性是
Tuning\u ID
,那么这就行了

有没有一个简单的方法来解决这个问题?

您可以:

1) 将XML加载到中,然后在文档的属性中导航(主要是)

2) 将XML加载到XMLDocument中,然后使用查询文档,通过创建导航器

3) 用一个简单的语法解析您的文档

4) 创建一组与Xml模式密切相关的类,并使用映射将文件映射到一组对象(这很好,但设置起来很棘手,因为XmlDeserializer可能有点难以配置)

如果我需要所有的数据,我会选择选项(4),可能会重新设计XML/模型类以使转换更容易

如果我需要一些值,我可能会选择(2),因为它只需要很少的代码。
参见(2)中的一些示例。

当然,有更简单的方法可以做到这一点。其中一个称为,您可以在其中将整个类转换为某种流,即XML和字节。您可以使用该类来完成此操作。下面是一个简单的例子:

假设您有一个要存储在XML文件中的类:

[Serializable] // you should mark the class as serializable
public class MyData
{
    public int Value { get; set; }
    public string Name { get; set; }
    public string[] SubItems { get; set; }
}
MyData data = new MyData();
data.Name = "TestName";
data.Value = 100;
data.SubItems = new string[] {"Item1", "Item2", "Item3"};
XmlSerializer ser = new XmlSerializer(typeof(MyData));
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
    ser.Serialize(file, data);
}
XDocument doc = XDocument.Load(fileName);
List<clsSessionTuningData> objSessionTuning = new List<clsSessionTuningData>()
foreach (var session in doc.Descendants("SessionTuningData"))
{
    XElement elem;
    clsSessionTuningData data = new clsSessionTuningData();
    elem = session.Element("Revision2");
    if (elem != null)
        data.bRevision2 = elem.Value;

    // and so on...
}
此代码将其转换为XML文件:

[Serializable] // you should mark the class as serializable
public class MyData
{
    public int Value { get; set; }
    public string Name { get; set; }
    public string[] SubItems { get; set; }
}
MyData data = new MyData();
data.Name = "TestName";
data.Value = 100;
data.SubItems = new string[] {"Item1", "Item2", "Item3"};
XmlSerializer ser = new XmlSerializer(typeof(MyData));
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
    ser.Serialize(file, data);
}
XDocument doc = XDocument.Load(fileName);
List<clsSessionTuningData> objSessionTuning = new List<clsSessionTuningData>()
foreach (var session in doc.Descendants("SessionTuningData"))
{
    XElement elem;
    clsSessionTuningData data = new clsSessionTuningData();
    elem = session.Element("Revision2");
    if (elem != null)
        data.bRevision2 = elem.Value;

    // and so on...
}
以下是生成的文件:

<?xml version="1.0"?>
<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Value>100</Value>
  <Name>TestName</Name>
  <SubItems>
    <string>Item1</string>
    <string>Item2</string>
    <string>Item3</string>
  </SubItems>
</MyData>
另一个选项是使用.NET framework中的一个专用类来读取XML文件:

[Serializable] // you should mark the class as serializable
public class MyData
{
    public int Value { get; set; }
    public string Name { get; set; }
    public string[] SubItems { get; set; }
}
MyData data = new MyData();
data.Name = "TestName";
data.Value = 100;
data.SubItems = new string[] {"Item1", "Item2", "Item3"};
XmlSerializer ser = new XmlSerializer(typeof(MyData));
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
    ser.Serialize(file, data);
}
XDocument doc = XDocument.Load(fileName);
List<clsSessionTuningData> objSessionTuning = new List<clsSessionTuningData>()
foreach (var session in doc.Descendants("SessionTuningData"))
{
    XElement elem;
    clsSessionTuningData data = new clsSessionTuningData();
    elem = session.Element("Revision2");
    if (elem != null)
        data.bRevision2 = elem.Value;

    // and so on...
}
XDocument doc=XDocument.Load(文件名);
列表objSessionTuning=新列表()
foreach(文档子体中的var会话(“SessionTuningData”))
{
Xelem;
clsSessionTuningData=新的clsSessionTuningData();
elem=会话元素(“修订2”);
if(elem!=null)
data.bRevision2=元素值;
//等等。。。
}

天哪!你听说过关于
序列化的事吗?
?是的,我听说过,但对此一无所知。@VishalSuthar我建议你从学习xml序列化开始。它简化了很多类似的事情:可以在这里找到一个例子:[[1]: