我如何在 C# 中真正读取具有多个属性的 xml 节点?

how can i literaly read in C# a xml node with multiple atributes?

对于我最近分配的一个大学项目,我需要在 C# 中创建一个酒店系统数据管理 WEB 应用程序,因此它具有从 xml 文件中获取的所有数据的众多功能之一将其保存在 sql 数据库中,因此在 xml 文件中我得到了多个节点,这意味着特定 tables 的数据在 SQL 数据库中,例如:

<Cadena>
<Codigo> CA001 </Codigo>
...
</Cadena>

好的,这不是我的问题,我的问题是我可以读取这样的节点:

<TipoHabitacion Cadena="CA001" Hotel="GT001">
.....
</TipoHabitacion>

我的意思是,我知道 table 是 "TipoHabitacion" 并且 table 的外键是 "Cadena=CA001" 和 "Hotel=GT001" 具有这些值,知道在同一文档中相同但具有不同外键的信息,我如何区分该信息,例如:

<TipoHabitacion Cadena="CA051" Hotel="GT781">
.....
</TipoHabitacion>

并用这些新值保存它?

您是在问如何获取元素的属性吗?如果是这样,这是一个 null-safe 解决方案:

string cadenaValue = null;
string hotelValue = null;
if (node.Attributes != null)
{
    var cadenaAttribute = node.Attributes["Cadena"];
    if (cadenaAttribute != null) 
        cadenaValue = cadenaAttribute.Value;

    var hotelAttribute = node.Attributs["Hotel"];
    if (hotelAttribute != null)
        hotelValue = hotelAttribute.Value;
}

if (cadenaValue != null)
{
    Console.WriteLine(cadenaValue);
}

if (hotelValue != null)
{
    Console.WriteLine(hotelValue);
}