我必须在大约400个不同的XML文件上迭代循环,每次我将获得不同的xml文件。
我在XML中有大约11个节点(全部为String
),我正在解析此XML并使用数据库中的实体框架存储XML元素的值(在Decimal
等不同的数据类型中,int
,string
,double
)
我不知道哪个xml节点会变为null,我不想为每个节点添加空检查。
有没有办法在循环中对整个XML文件 实现公共空检查,所以如果任何节点为空,我可以将其分配给默认值各个实体中相应数据类型的值。有些内容如下所示的代码片段: -
foreach (XmlNode node in tableElements)
{
dcSearchTerm searchTermEntity = new dcSearchTerm();
//Reference keywords: creation & assignment
int IDRef = 0, salesRef = 0, visitsRef = 0, saleItemsRef = 0;
DateTime visitDateRef = new DateTime();
decimal revenueRef = 0;
int.TryParse(node["id"].InnerText, out IDRef);
searchTermEntity.SearchTerm = node["Search_x0020_Term"].InnerText;
searchTermEntity.ReferrerDomain = node["Referrer_x0020_Domain"].InnerText;
if (node["Country"] == null)
{
searchTermEntity.Country = "";
}
else
{
searchTermEntity.Country = node["Country"].InnerText;
}
DateTime.TryParse(node["Visit_x0020_Date"].InnerText, out visitDateRef);
searchTermEntity.VisitEntryPage = node["Visit_x0020_Entry_x0020_Page"].InnerText;
int.TryParse(node["Sales"].InnerText, out salesRef);
int.TryParse(node["Visits"].InnerText, out visitsRef);
decimal.TryParse(node["Revenue"].InnerText, out revenueRef);
int.TryParse(node["Sale_x0020_Items"].InnerText, out saleItemsRef);
// assigning reference values to the entity
searchTermEntity.ID = IDRef;
searchTermEntity.VisitDate = visitDateRef;
searchTermEntity.Sales = salesRef;
searchTermEntity.Visits = visitsRef;
searchTermEntity.Revenue = revenueRef;
searchTermEntity.SaleItems = saleItemsRef;
searches.Add(searchTermEntity);
return searches;
}
P.S .: - 这是我关于SO的第一个问题,请随时询问更多细节 等待大量的建议! :)
答案 0 :(得分:3)
好的,这是扩展类,它为Strings和XmlNodes添加方法:
public static class MyExtensions
{
// obviously these ToType methods can be implemented with generics
// to further reduce code duplication
public static int ToInt32(this string value)
{
Int32 result = 0;
if (!string.IsNullOrEmpty(value))
Int32.TryParse(value, out result);
return result;
}
public static decimal ToDecimal(this string value)
{
Decimal result = 0M;
if (!string.IsNullOrEmpty(value))
Decimal.TryParse(value, out result);
return result;
}
public static int GetInt(this XmlNode node, string key)
{
var str = node.GetString(key);
return str.ToInt32();
}
public static string GetString(this XmlNode node, string key)
{
if (node[key] == null || String.IsNullOrEmpty(node[key].InnerText))
return null;
else
return node.InnerText;
}
// implement GetDateTime/GetDecimal as practice ;)
}
现在我们可以重写您的代码:
foreach (XmlNode node in tableElements)
{
// DECLARE VARIABLES WHEN YOU USE THEM
// DO NOT DECLARE THEM ALL AT THE START OF YOUR METHOD
// http://programmers.stackexchange.com/questions/56585/where-do-you-declare-variables-the-top-of-a-method-or-when-you-need-them
dcSearchTerm searchTermEntity = new dcSearchTerm()
{
ID = node.GetInt("id"),
SearchTerm = node.GetString("Search_x0020_Term"),
ReferrerDomain = node.GetString("Referrer_x0020_Domain"),
Country = node.GetString("Country"),
VisitDate = node.GetDateTime("Visit_x0020_Date"),
VisitEntryPage = node.GetString("Visit_x0020_Entry_x0020_Page"),
Sales = node.GetInt("Sales"),
Visits = node.GetInt("Visits"),
Revenue = node.GetDecimal("Revenue"),
SaleItems = node.GetDecimal("Sale_x0020_Items")
};
searches.Add(searchTermEntity);
return searches;
}
不要忘记实现GetDateTime和GetDecimal扩展 - 我已经把它们留给了你;)。
答案 1 :(得分:0)
您可以使用如下所示的monad样式扩展方法。提供的样本仅作用于结构。您可以修改它以用于所有类型。
public static class NullExtensions
{
public delegate bool TryGetValue<T>(string input, out T value);
public static T DefaultIfNull<T>(this string value, TryGetValue<T> evaluator, T defaultValue) where T : struct
{
T result;
if (evaluator(value, out result))
return result;
return defaultValue;
}
public static T DefaultIfNull<T>(this string value, TryGetValue<T> evaluator) where T : struct
{
return value.DefaultIfNull(evaluator, default(T));
}
}
示例:强>
string s = null;
bool result = s.DefaultIfNull<bool>(bool.TryParse, true);
int r = s.DefaultIfNull<int>(int.TryParse);