如何在 xml 和 c# -2 中处理 null
How to handle null in xml and c# -2
让我们认为这是自动生成的 xml 文件,在第二个地址标签中没有任何内容。为空。
如果它不是 null ,我们将为地址创建一个列表,然后继续 class 声明,如下所示。但如果我们这样做,由于地址标记为空,它会给我们一个错误。
如果可能的话,我想添加一个 if 语句,说明
如果为 null ,则将其存储为字符串。
如果它不为 null ,则使用列表。
你能在代码部分告诉我怎么做吗?
此致
xml 样本:
<?xml version="1.0"?>
<XMLCD>
<Personnel>
<AddressDirectory>
<Owner>Jerry</Owner>
<Age>29</Age>
<Company>123</Company>
<Address>
<ST>
<HouseNo>1</HouseNo>
<StreetName>5th</StreetName>
<Town>Elmsford</Town>
</ST>
</Address>
<Address>
</Address>
</AddressDirectory>
<AddressDirectory>
<Owner>Joe</Owner>
<Age>24</Age>
<Company>456</Company>
<Address>
<ST>
<HouseNo>1</HouseNo>
<StreetName>10Blvd</StreetName>
<Town>StMichael</Town>
</ST>
</Address>
<Address>
</Address>
</AddressDirectory>
</Personnel>
</XMLCD>
我的代码:
OpenFileDialog ofd = new OpenFileDialog();
opnFileName = ofd.FileName;
XmlSerializer deserializer = new XmlSerializer(typeof(XMLCD));
TextReader reader = new StreamReader(this.opnFileName);
object obj = deserializer.Deserialize(reader);
XMLCD XmlData = (XMLCD)obj;
reader.Close();
public class XMLCD
{
[XmlElement("Personnel")]
public List<Personnel> PersonnelList = new List<Personnel>();
public class Personnel
{
[XmlElement("AddressDirectory")]
public List<AddressDirectory> AddressDirectoryList = new List<AddressDirectory>();
public class AddressDirectory
{
[XmlElement("Owner")]
public string Owner{ get; set; }
[XmlElement("Age")]
public string Age{ get; set; }
[XmlElement("Company")]
public string Company{ get; set; }
[XmlElement("Address")]
public List<Address> AddressList = new List<Address>();
public class Address
{
[XmlElement("ST")]
public List<ST> STList = new List<ST>();
public class ST
{
[XmlElement("HouseNo")]
public string HouseNo{ get; set; }
[XmlElement("StreetName")]
public string StreetName{ get; set; }
[XmlElement("Town")]
public string Town{ get; set; }
}
}
}
}
上面的代码仅适用于 xml 没有空地址标签的代码。当 xml 代码具有如上所示的空地址标签时,它会给出错误。
如果您只控制反序列化,您可以在反序列化后使用 Linq .RemoveAll() 扩展方法简单地删除不需要的空 Address
条目:
XmlSerializer deserializer = new XmlSerializer(typeof(XMLCD));
XMLCD XmlData;
using (var reader = new StreamReader(this.opnFileName))
{
XmlData = deserializer.Deserialize(reader) as XMLCD;
}
// loop over all Personnel to cleanse their AddressDirectoryList.AddressList
foreach (Personnel p in XmlData.PersonnelList)
{
foreach (AddressDirectory ad in p.AddressDirectoryList)
{
// RemoveAll predicate checks if ALL properties are null or empty
ad.AddressList.RemoveAll(a =>
(string.IsNullOrEmpty(a.HouseNo) &&
string.IsNullOrEmpty(a.StreetName) &&
string.IsNullOrEmpty(a.City))
);
}
}
基本上 RemoveAll() 方法只是遍历 AddressList
集合中的所有项目。
为了简化验证,您可以扩展 Address
class 以验证自身:
public class Address
{
[XmlElement("HouseNo")]
public string HouseNo { get; set; }
[XmlElement("StreetName")]
public string StreetName{ get; set; }
[XmlElement("City")]
public string City{ get; set; }
[XmlIgnore]
public bool IsValid
{
get
{
return !string.IsNullOrEmpty(this.HouseNo)
&& !string.IsNullOrEmpty(this.StreetName)
&& !string.IsNullOrEmpty(this.City);
}
}
}
这将允许在您的 RemoveAll 谓词中使用单个条件:
ad.AddressList.RemoveAll(a => !a.IsValid);
一种非 Linq 替代方法可能是使用 Xslt 过滤掉空地址元素来转换源 Xml。
让我们认为这是自动生成的 xml 文件,在第二个地址标签中没有任何内容。为空。
如果它不是 null ,我们将为地址创建一个列表,然后继续 class 声明,如下所示。但如果我们这样做,由于地址标记为空,它会给我们一个错误。
如果可能的话,我想添加一个 if 语句,说明
如果为 null ,则将其存储为字符串。 如果它不为 null ,则使用列表。
你能在代码部分告诉我怎么做吗?
此致
xml 样本:
<?xml version="1.0"?>
<XMLCD>
<Personnel>
<AddressDirectory>
<Owner>Jerry</Owner>
<Age>29</Age>
<Company>123</Company>
<Address>
<ST>
<HouseNo>1</HouseNo>
<StreetName>5th</StreetName>
<Town>Elmsford</Town>
</ST>
</Address>
<Address>
</Address>
</AddressDirectory>
<AddressDirectory>
<Owner>Joe</Owner>
<Age>24</Age>
<Company>456</Company>
<Address>
<ST>
<HouseNo>1</HouseNo>
<StreetName>10Blvd</StreetName>
<Town>StMichael</Town>
</ST>
</Address>
<Address>
</Address>
</AddressDirectory>
</Personnel>
</XMLCD>
我的代码:
OpenFileDialog ofd = new OpenFileDialog();
opnFileName = ofd.FileName;
XmlSerializer deserializer = new XmlSerializer(typeof(XMLCD));
TextReader reader = new StreamReader(this.opnFileName);
object obj = deserializer.Deserialize(reader);
XMLCD XmlData = (XMLCD)obj;
reader.Close();
public class XMLCD
{
[XmlElement("Personnel")]
public List<Personnel> PersonnelList = new List<Personnel>();
public class Personnel
{
[XmlElement("AddressDirectory")]
public List<AddressDirectory> AddressDirectoryList = new List<AddressDirectory>();
public class AddressDirectory
{
[XmlElement("Owner")]
public string Owner{ get; set; }
[XmlElement("Age")]
public string Age{ get; set; }
[XmlElement("Company")]
public string Company{ get; set; }
[XmlElement("Address")]
public List<Address> AddressList = new List<Address>();
public class Address
{
[XmlElement("ST")]
public List<ST> STList = new List<ST>();
public class ST
{
[XmlElement("HouseNo")]
public string HouseNo{ get; set; }
[XmlElement("StreetName")]
public string StreetName{ get; set; }
[XmlElement("Town")]
public string Town{ get; set; }
}
}
}
}
上面的代码仅适用于 xml 没有空地址标签的代码。当 xml 代码具有如上所示的空地址标签时,它会给出错误。
如果您只控制反序列化,您可以在反序列化后使用 Linq .RemoveAll() 扩展方法简单地删除不需要的空 Address
条目:
XmlSerializer deserializer = new XmlSerializer(typeof(XMLCD));
XMLCD XmlData;
using (var reader = new StreamReader(this.opnFileName))
{
XmlData = deserializer.Deserialize(reader) as XMLCD;
}
// loop over all Personnel to cleanse their AddressDirectoryList.AddressList
foreach (Personnel p in XmlData.PersonnelList)
{
foreach (AddressDirectory ad in p.AddressDirectoryList)
{
// RemoveAll predicate checks if ALL properties are null or empty
ad.AddressList.RemoveAll(a =>
(string.IsNullOrEmpty(a.HouseNo) &&
string.IsNullOrEmpty(a.StreetName) &&
string.IsNullOrEmpty(a.City))
);
}
}
基本上 RemoveAll() 方法只是遍历 AddressList
集合中的所有项目。
为了简化验证,您可以扩展 Address
class 以验证自身:
public class Address
{
[XmlElement("HouseNo")]
public string HouseNo { get; set; }
[XmlElement("StreetName")]
public string StreetName{ get; set; }
[XmlElement("City")]
public string City{ get; set; }
[XmlIgnore]
public bool IsValid
{
get
{
return !string.IsNullOrEmpty(this.HouseNo)
&& !string.IsNullOrEmpty(this.StreetName)
&& !string.IsNullOrEmpty(this.City);
}
}
}
这将允许在您的 RemoveAll 谓词中使用单个条件:
ad.AddressList.RemoveAll(a => !a.IsValid);
一种非 Linq 替代方法可能是使用 Xslt 过滤掉空地址元素来转换源 Xml。