c# - 从 XML 节点读取特定值
问题描述
我有一个 XML 文件,我需要读取 XML 文件中特定节点的值。
代码:
public static string GetElementByName(string responseContent,string attributeName, string attributeValue)
{
string result = "";
XmlTextReader textReader = new XmlTextReader(new System.IO.StringReader(responseContent));
while (textReader.Read())
{
switch (textReader.NodeType)
{
case XmlNodeType.Element:
if (!textReader.IsEmptyElement)
{
if (textReader.GetAttribute(attributeName) == attributeValue)
{
result = textReader.ReadInnerXml();
}
}
break;
case XmlNodeType.Text:
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
break;
case XmlNodeType.Comment:
break;
case XmlNodeType.EndElement:
break;
}
}
return result;
}
在这里,我将 xml 内容作为字符串 (responseContent)、属性名称和属性值传递,以获取该特定节点的实际值。如果 xml 节点像
<Reference ReferenceType="ABC" AssignedBy="Buyer">123</Reference>
<Reference ReferenceType="DEF" AssignedBy="Buyer">456</Reference>
var value1 = GetElementByName(xmlContent,"ReferenceType","ABC"); // value1 = 123
var value2 = GetElementByName(xmlContent,"ReferenceType","DEF"); // value2 = 456
但是现在要覆盖一个新的场景。
我有一个像
<Component>
<Classification ClassificationType="BOOK">
<SubClassification SubClassificationType="FICTION"/>
</Classification>
<Reference ReferenceType="ABC">123</Reference>
</Component>
<Component>
<Classification ClassificationType="MOVIES">
<SubClassification SubClassificationType="ROMANCE"/>
</Classification>
<Reference ReferenceType="ABC">456</Reference>
</Component>
现在,我需要调用一个函数,一个应该返回 BOOK 分类的值 123,另一个应该返回 MOVIES 分类的值 456。
如何检查节点然后提取值。
更新#1:
让我们说如果
我调用 GetElement(xmlContent, "BOOK", "ReferenceType", "ABC"),然后我应该得到 123。
我调用 GetElement(xmlContent, "MOVIES", "ReferenceType", "ABC"),那么我应该得到 456。
解决方案
不错的做法,
首先,在 c# 模型中转换您的 XML
这是使用 xmltocsharp 生成的 C# 模型类的方式
[XmlRoot(ElementName = "SubClassification")]
public class SubClassification
{
[XmlAttribute(AttributeName = "SubClassificationType")]
public string SubClassificationType { get; set; }
}
[XmlRoot(ElementName = "Classification")]
public class Classification
{
[XmlElement(ElementName = "SubClassification")]
public SubClassification SubClassification { get; set; }
[XmlAttribute(AttributeName = "ClassificationType")]
public string ClassificationType { get; set; }
}
[XmlRoot(ElementName = "Reference")]
public class Reference
{
[XmlAttribute(AttributeName = "ReferenceType")]
public string ReferenceType { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Component")]
public class Component
{
[XmlElement(ElementName = "Classification")]
public Classification Classification { get; set; }
[XmlElement(ElementName = "Reference")]
public Reference Reference { get; set; }
}
[XmlRoot(ElementName = "Root")]
public class Root
{
[XmlElement(ElementName = "Component")]
public List<Component> Component { get; set; }
}
那么这里有一个辅助方法可以解决问题
public static string GetValue(string data, string classificationTypeValue, string referenceTypeValue)
{
// Serializing XML here
Root root;
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(Root));
using (StringReader sr = new StringReader(data))
{
root = (Root)ser.Deserialize(sr);
}
if (root != null)
{
// First we'll get a components which have given classificationType E.g (Books,Movies)
List<Component> componentWithReferenceType = root.Component.Where(x => x.Classification.ClassificationType == classificationTypeValue).ToList();
// Now once we have componets with given classificationType all we have to do is grab it's first child and return it's referenceText
return componentWithReferenceType.First(x => x.Reference.ReferenceType == referenceTypeValue).Reference.Text;
}
return string.Empty;
}
推荐阅读
- c# - 建立与 SQL Server.Asp.net 的连接时发生与网络相关或特定于实例的错误
- php - Laravel API 正在接收写成“null”而不是空字段的引号之间的字符串
- python-3.x - Pytorch:图像标签
- sql - SQL在注册后映射用户
- wordpress - 注销/登录菜单 Wordpress
- optimization - 用两个不同的模型函数同时拟合两组数据,给出一个唯一的最优参数
- c# - ASP.NET 运行时错误:无法加载文件程序集“System.Security.Cryptography.Algorithms”
- java - Spring boot:努力验证嵌套实体 - 我只需要验证 1 个唯一属性
- dbcontext - 多个 DbContext。“在前一个操作完成之前,第二个操作在此上下文中开始。”
- android - 以编程方式在不使用意图 android 的情况下导入 vcf 文件