我有一个XML文件,我需要读取XML文件中特定节点的值。
代码:
sendToApi
在这里,我将xml内容作为字符串(responseContent),属性名称和属性值传递,以获取该特定节点的实际值。如果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;
}
<Reference ReferenceType="ABC" AssignedBy="Buyer">123</Reference>
<Reference ReferenceType="DEF" AssignedBy="Buyer">456</Reference>
var value1 = GetElementByName(xmlContent,"ReferenceType","ABC"); // value1 = 123
但是现在要涵盖一个新场景。
我有一个xml
var value2 = GetElementByName(xmlContent,"ReferenceType","DEF"); // value2 = 456
现在,我需要调用一个函数,一个应返回BOOK分类的值123,另一个应返回MOVIES分类的456的值。
如何检查节点,然后提取值。
更新#1:
让我们说
我调用GetElement(xmlContent,“ BOOK”,“ ReferenceType”,“ ABC”),那么我应该得到123。
我打电话给GetElement(xmlContent,“ MOVIES”,“ ReferenceType”,“ ABC”),那么我应该得到456。
答案 0 :(得分:1)
好的方法,
首先,在c#模型中转换XML
这是您的C#模型类Generated using xmltocsharp
[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;
}
答案 1 :(得分:0)
您需要使用类似Classification
的功能来读取Component
内部的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;
}
输出: