C# 从XML节点读取特定值
我有一个XML文件,需要读取XML文件中特定节点的值 代码: 在这里,我将xml内容作为字符串(responseContent)、属性名和属性值传递,以获取特定节点的实际值。如果xml节点类似于C# 从XML节点读取特定值,c#,xml,C#,Xml,我有一个XML文件,需要读取XML文件中特定节点的值 代码: 在这里,我将xml内容作为字符串(responseContent)、属性名和属性值传递,以获取特定节点的实际值。如果xml节点类似于 <Reference ReferenceType="ABC" AssignedBy="Buyer">123</Reference> <Reference ReferenceType="DEF" AssignedBy="Buyer">456</Reference&
<Reference ReferenceType="ABC" AssignedBy="Buyer">123</Reference>
<Reference ReferenceType="DEF" AssignedBy="Buyer">456</Reference>
现在,我需要调用一个函数,一个应该返回图书分类的值123,另一个应该返回电影分类的值456
如何检查节点,然后提取值
更新#1:
假设
我调用GetElement(xmlContent,“BOOK”,“ReferenceType”,“ABC”),然后我应该得到123
我调用GetElement(xmlcont,“MOVIES”,“ReferenceType”,“ABC”),然后我应该得到456。您需要使用
ReadToNextSibling()
之类的函数来读取组件中分类的同级
public static string GetElementByName(string responseContent, string classification, 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.Name == "Classification" && textReader.GetAttribute("ClassificationType") == classification)
{
textReader.ReadToNextSibling("Reference");
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;
}
输出:
很好的方法
首先,在c#模型中转换XML
这是你的C#Model课程的教学方法
[XmlRoot(ElementName=“子分类”)]
公共类子分类
{
[xmltattribute(AttributeName=“SubClassificationType”)]
公共字符串子分类类型{get;set;}
}
[XmlRoot(ElementName=“分类”)]
公共类分类
{
[XmlElement(ElementName=“子分类”)]
公共子分类子分类{get;set;}
[XmlAttribute(AttributeName=“ClassificationType”)]
公共字符串分类类型{get;set;}
}
[XmlRoot(ElementName=“Reference”)]
公开课参考
{
[XmlAttribute(AttributeName=“ReferenceType”)]
公共字符串引用类型{get;set;}
[XmlText]
公共字符串文本{get;set;}
}
[XmlRoot(ElementName=“Component”)]
公共类组件
{
[xmlement(ElementName=“分类”)]
公共分类{get;set;}
[xmlement(ElementName=“Reference”)]
公共引用{get;set;}
}
[XmlRoot(ElementName=“Root”)]
公共类根
{
[xmlement(ElementName=“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;
}
公共静态字符串GetValue(字符串数据、字符串分类类型值、字符串引用类型值)
{
//在这里序列化XML
根;
System.Xml.Serialization.XmlSerializer ser=new System.Xml.Serialization.XmlSerializer(typeof(Root));
使用(StringReader sr=新StringReader(数据))
{
根=(根)序列反序列化(sr);
}
if(root!=null)
{
//首先,我们将得到一个给定分类类型的组件,例如(书籍、电影)
列出引用类型为root.Component.Where(x=>x.Classification.ClassificationType==classificationTypeValue.ToList())的组件;
//现在,一旦我们有了具有给定classificationType的组件,我们所要做的就是抓住它的第一个子元素并返回它的referenceText
返回componentWithReferenceType.First(x=>x.Reference.ReferenceType==referenceTypeValue);
}
返回字符串。空;
}
您能更具体地说明您在输出中需要什么吗?您好@MihirDave我已经更新了问题。谢谢您的回答,但是@Mihir Dave给出的答案更容易被团队接受。@Ranjith很高兴它帮助了您和您的团队。!:-)
public static string GetElementByName(string responseContent, string classification, 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.Name == "Classification" && textReader.GetAttribute("ClassificationType") == classification)
{
textReader.ReadToNextSibling("Reference");
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;
}
[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;
}