gpt4 book ai didi

C# 用相应数据类型的默认值替换空 XML 节点

转载 作者:太空狗 更新时间:2023-10-30 01:34:49 25 4
gpt4 key购买 nike

我必须在大约 400 个不同的 XML 文件上迭代一个循环,每次我都会得到不同的 xml 文件。

我在 XML 中有大约 11 个节点(全部以 String 形式出现),我正在解析此 XML 并使用数据库中的 Entity Framework 存储 XML 元素的值(在不同的数据类型中,如 >十进制, int, string, double)

我不知道哪个 xml 节点会变成 null,我不想为每个节点添加 null 检查。

有没有办法在循环中为整个 XML 文件实现一个通用的 null 检查,所以如果任何节点为 null,我可以将它分配给默认值各自实体中各自数据类型的值。类似于下面显示的代码片段:-

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 的第一个问题,请随时询问更多详细信息 等待大量的建议! :)

最佳答案

好的,这是向字符串和 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 扩展 - 我已经把它们留给你了 ;)。

关于C# 用相应数据类型的默认值替换空 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28871238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com