我有这样的XML。
<language code="eng">
<label lang="tr">İngilizce</label>
<label lang="en">English</label>
</language>
这是一个例子。我有402个节点。
我正在尝试使用C#解析这个XML文件,就像代码一样,我只能得到语言代码值。
这是代码。
var languageCode = doc.Root
.Elements("language")
.Select(x => new XmlModel
{
Code = (string) x.Attribute("code"),
})
.ToList();
但是我需要在相同的代码中获得标签lang =“tr”并标记lang =“en”内部文本(Ingilizce,英语)。我没有这样做。
如何更改我的代码?谢谢你的帮助。 :)
编辑:
var languageCodeAndValue = doc.Root
.Elements("language")
.SelectMany(x => x.Elements("label").Select(item => new Tuple<string, string>(item.Attribute("lang").Value, item.Value)))
.ToList();
这段代码给我一个这样的列表:
tr,Turkce
en,Turkish
tr,Ingilizce
en,English
但我需要
tur,tr,Turkce
tur,en,Turkish
eng,tr,Ingilizce
eng,en,English
答案 0 :(得分:1)
If I right understood you, just try this:
var languageCodeAndValue = doc.Root
.Elements("language")
.SelectMany(x => x.Elements("label").Select(item => new Tuple<string, string, string>(x.Attribute("code").Value, item.Attribute("lang").Value, item.Value)))
.ToList();
Your languageCodeAndValue
would be contain a language code, it attribute and text: ("eng", "tr", "İngilizce"), ("eng", "en", "English")
And retrive values:
foreach (var value in languageCodeAndValue)
{
Console.WriteLine(value.Item1);
Console.WriteLine(value.Item2);
Console.WriteLine(value.Item3);
}
答案 1 :(得分:0)
尝试这样的事情:
$POSTParams = @{
client_id = "client-id-value"
secret = "secret-value"
access_token = "access-token-value"
}
Invoke-WebRequest -Uri "https://tartan.plaid.com/balance" -Method POST -Body $POSTParams
答案 2 :(得分:0)
解析xml的另一种方法是使用XmlSerializer
。如果您将xml复制到剪贴板并选择Past Special&gt;,Visual Studio可以根据您的xml为您创建类。将XML粘贴为类。
然后你可以反序列化xml,你将得到一个language
对象,其数组为label
s。
void Main()
{
string xml = @"<language code=""eng"">
<label lang=""tr"">İngilizce</label>
<label lang=""en"">English</label>
</language>";
XmlSerializer ser = new XmlSerializer(typeof(language));
using (MemoryStream ms = new MemoryStream())
{
XDocument.Parse(xml).Save(ms);
ms.Position = 0;
var lang = (language)ser.Deserialize(ms);
Console.WriteLine(lang.code);
foreach (languageLabel lbl in lang.label)
{
Console.WriteLine(lbl.lang);
Console.WriteLine(lbl.Value);
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class language
{
private languageLabel[] labelField;
private string codeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("label")]
public languageLabel[] label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class languageLabel
{
private string langField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lang
{
get
{
return this.langField;
}
set
{
this.langField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}