我正在尝试将项目从XML插入到数据库表中。
为此,我创建了局部变量,并希望将XML标记插入到它们中,以便稍后我可以将这些局部变量插入到数据库中。
我这样做的方式是反序列化XML:
static void Main(string[] args)
{
try
{
List<FolkbokforingspostTYPE> deserializedList = new List<FolkbokforingspostTYPE>();
deserializedList = Deserialize<List<FolkbokforingspostTYPE>>();
var First = deserializedList.Select(item => item.Personpost.Namn.Fornamn).ToList();
foreach (var o in First)
{
Console.WriteLine("Namn: " + o);
}
//local variable that contains the the value of a XML tag.
string FirstName = First;
}
PersonalIdentityNumber... deserializedList contains everything if you need it
catch (Exception)
{
throw;
}
Console.ReadKey();
}
private static T Deserialize<T>() where T : new()
{
// Create an instance of T
T ReturnListOfT = CreateInstance<T>();
// Create a new file stream for reading the XML file
using (FileStream ReadFileStream = new FileStream("navetout.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Construct a XmlSerializer and use it
// to serialize the data from the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Deserialize the hashtable from the file
ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
// return the Deserialized data.
return ReturnListOfT;
}
// function to create instance of T
public static T CreateInstance<T>() where T : new()
{
return (T)Activator.CreateInstance(typeof(T));
}
问题是我无法转换list<string>
(从XML中获取值)以分配给本地变量。
从上面的代码中你可以看到我试过这个:
string FirstName = First;
但我得到错误:
无法隐式转换类型 &#39; System.Collections.Generic.List&#39;到&#39;字符串&#39;
我知道为什么会收到错误,我只是想知道是否有办法绕过它?我可以以某种方式将列表分配给局部变量吗?
这是我反序列化XML的方式:
[XmlRoot(ElementName = "Sekretessmarkering")]
public class Sekretessmarkering
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "PersonId")]
public class PersonId
{
[XmlElement(ElementName = "PersonNr")]
public string PersonNr { get; set; }
}
[XmlRoot(ElementName = "HanvisningsPersonNr")]
public class HanvisningsPersonNr
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "Mellannamn")]
public class Mellannamn
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "Aviseringsnamn")]
public class Aviseringsnamn
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "Namn")]
public class Namn
{
[XmlElement(ElementName = "Tilltalsnamnsmarkering")]
public string Tilltalsnamnsmarkering { get; set; }
[XmlElement(ElementName = "Fornamn")]
public string Fornamn { get; set; }
[XmlElement(ElementName = "Mellannamn")]
public Mellannamn Mellannamn { get; set; }
[XmlElement(ElementName = "Efternamn")]
public string Efternamn { get; set; }
[XmlElement(ElementName = "Aviseringsnamn")]
public Aviseringsnamn Aviseringsnamn { get; set; }
}
[XmlRoot(ElementName = "ForsamlingKod")]
public class ForsamlingKod
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "Folkbokforing")]
public class Folkbokforing
{
[XmlElement(ElementName = "Folkbokforingsdatum")]
public string Folkbokforingsdatum { get; set; }
[XmlElement(ElementName = "LanKod")]
public string LanKod { get; set; }
[XmlElement(ElementName = "KommunKod")]
public string KommunKod { get; set; }
[XmlElement(ElementName = "ForsamlingKod")]
public ForsamlingKod ForsamlingKod { get; set; }
[XmlElement(ElementName = "Fastighetsbeteckning")]
public string Fastighetsbeteckning { get; set; }
[XmlElement(ElementName = "FiktivtNr")]
public string FiktivtNr { get; set; }
}
[XmlRoot(ElementName = "CareOf")]
public class CareOf
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "Utdelningsadress1")]
public class Utdelningsadress1
{
[XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Nil { get; set; }
}
[XmlRoot(ElementName = "Folkbokforingsadress")]
public class Folkbokforingsadress
{
[XmlElement(ElementName = "CareOf")]
public CareOf CareOf { get; set; }
[XmlElement(ElementName = "Utdelningsadress1")]
public Utdelningsadress1 Utdelningsadress1 { get; set; }
[XmlElement(ElementName = "Utdelningsadress2")]
public string Utdelningsadress2 { get; set; }
[XmlElement(ElementName = "PostNr")]
public string PostNr { get; set; }
[XmlElement(ElementName = "Postort")]
public string Postort { get; set; }
}
[XmlRoot(ElementName = "Riksnycklar")]
public class Riksnycklar
{
[XmlElement(ElementName = "FastighetsId")]
public string FastighetsId { get; set; }
[XmlElement(ElementName = "AdressplatsId")]
public string AdressplatsId { get; set; }
[XmlElement(ElementName = "LagenhetsId")]
public string LagenhetsId { get; set; }
}
[XmlRoot(ElementName = "Adresser")]
public class Adresser
{
[XmlElement(ElementName = "Folkbokforingsadress")]
public Folkbokforingsadress Folkbokforingsadress { get; set; }
[XmlElement(ElementName = "Riksnycklar")]
public Riksnycklar Riksnycklar { get; set; }
}
[XmlRoot(ElementName = "HemortSverige")]
public class HemortSverige
{
[XmlElement(ElementName = "FodelselanKod")]
public string FodelselanKod { get; set; }
[XmlElement(ElementName = "Fodelseforsamling")]
public string Fodelseforsamling { get; set; }
}
[XmlRoot(ElementName = "Fodelse")]
public class Fodelse
{
[XmlElement(ElementName = "HemortSverige")]
public HemortSverige HemortSverige { get; set; }
}
[XmlRoot(ElementName = "Medborgarskap")]
public class Medborgarskap
{
[XmlElement(ElementName = "MedborgarskapslandKod")]
public string MedborgarskapslandKod { get; set; }
[XmlElement(ElementName = "Medborgarskapsdatum")]
public string Medborgarskapsdatum { get; set; }
}
[XmlRoot(ElementName = "Personpost")]
public class Personpost
{
[XmlElement(ElementName = "PersonId")]
public PersonId PersonId { get; set; }
[XmlElement(ElementName = "HanvisningsPersonNr")]
public HanvisningsPersonNr HanvisningsPersonNr { get; set; }
[XmlElement(ElementName = "Namn")]
public Namn Namn { get; set; }
[XmlElement(ElementName = "Folkbokforing")]
public Folkbokforing Folkbokforing { get; set; }
[XmlElement(ElementName = "Adresser")]
public Adresser Adresser { get; set; }
[XmlElement(ElementName = "Fodelse")]
public Fodelse Fodelse { get; set; }
[XmlElement(ElementName = "Medborgarskap")]
public Medborgarskap Medborgarskap { get; set; }
}
[XmlRoot(ElementName = "FolkbokforingspostTYPE")]
public class FolkbokforingspostTYPE
{
[XmlElement(ElementName = "Sekretessmarkering")]
public Sekretessmarkering Sekretessmarkering { get; set; }
[XmlElement(ElementName = "Personpost")]
public Personpost Personpost { get; set; }
}
}
答案 0 :(得分:0)
嗯,它包含多个值,如果我尝试使用FirstOrDefault(),那么它只返回值的第一个字母..
从这条评论中,您的List<string>
似乎将单个字母作为所有项目共同构建单词的项目。
此时您关于如何List to string
的问题有点意义。
而不是string FirstName = First;
使用string FirstName = String.Join("", First);
String.Join()
将加入List<string>
中的所有项目并返回string
。
答案 1 :(得分:0)
我假设您要加载xml,当反序列化返回字符串列表并且您希望此字符串列表存储在数据库中时BUT因为字符串列表不是数据库可以处理您打算的数据类型将其存储为字符串。
正如@ C4ud3x所说,string.Join会将你的列表变成一个字符串。但我也假设您希望在某些情况下将其转换回数据库中的列表。
而不是字符串FirstName = First;使用字符串FirstName = String.Join(&#34;&#34;,First);
将其更改为String.Join(&#34; *&#34;,First);其中*是您要用来分隔数据的特殊字符。
Ex:First = [&#39; John&#39;,&#39; Doe&#39;,&#39; Foo&#39;,&#39; Bar&#39;];
string FirstName = String.Join("*", First); //John*Doe*Foo*Bar
然后将其存储到数据库中。
要将其转换回列表,您可以使用string.Split
string[] First = FirstName.Split('*', StringSplitOptions.RemoveEmptyEntries); //['John','Doe','Foo','Bar'];
或者您可以将数据序列化为JSON,这很容易反序列化回对象。
仅使用JSON的一个数据库示例是Firebase。