<Tasks>
<AuxFiles>
<FileType AttachmentType='csv' FileFormat ='*.csv'>
</AuxFiles>
</Tasks>
AttachmentType
を知っている場合、FileFormat
を取得するためのC#の構文は何ですか?Cを使用してXMLを読む#
すべてのヘルプは常に高く評価されています。
<Tasks>
<AuxFiles>
<FileType AttachmentType='csv' FileFormat ='*.csv'>
</AuxFiles>
</Tasks>
AttachmentType
を知っている場合、FileFormat
を取得するためのC#の構文は何ですか?Cを使用してXMLを読む#
すべてのヘルプは常に高く評価されています。
すると、このコードを試してみてください。
string fileFormat = string.Empty;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(fileName);
XmlNodeList auxFilesList = xDoc.GetElementsByTagName("AuxFiles");
for (int i = 0; i < auxFilesList.Count; i++)
{
XmlNode item = classList.Item(i);
if (item.Attributes["AttachmentType"].Value == "csv")
{
fileFormat = item.Attributes["FileFormat"].Value;
}
}
XElement
とそのクエリをサポートしています。
XElement element = XElement.Parse(@"<Tasks>
<AuxFiles>
<FileType AttachmentType='csv' FileFormat ='*.csv' />
</AuxFiles>
</Tasks>");
string format = element.Descendants("FileType")
.Where(x => x.Attribute("AttachmentType").Value == "csv")
.Select(x => x.Attribute("FileFormat").Value)
.First();
Console.WriteLine(format);
Jon Skeetのソリューションは、これよりも堅牢です。タイプが一致しないか、要素にFileFormat属性がない場合、NullReferenceExceptionが発生しないためです。しかし、その考え方は同じです。 – carlosfigueira
私はXMLにLINQを使用したい:
var doc = XDocument.Load("file.xml");
var format = doc.Descendants("FileType")
.Where(x => (string) x.Attribute("AttachmentType") == type)
.Select(x => (string) x.Attribute("FileFormat"))
.FirstOrDefault();
一致する要素または一致AttachmentType
との最初のFileType
がFileFormat
属性を持っていない場合が存在しない場合、これはnull
を与えるだろう。
XPATHを使用すると、XMLファイルの任意の要素を照会できます。
このULRを参照してください。また、このSOポストhttp://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm
チェック: "どのようにピアのXMLNode .NETを照会するには、"
それを行う別の方法があります:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("path\\to\\file.xml");
// Select the node where AttachmentType='csv'
XmlNode node = xDoc.SelectSingleNode("/Tasks/AuxFiles/FileType[@AttachmentType='csv']");
// Read the value of the Attribute 'FileFormat'
var fileFormat = node.Attributes["FileFormat"].Value;
あなたの質問です:このxmlファイルを指定すると、私はFileformat属性値指定されたAttachmentType?だから、 "CSV"を探すときに "* .csv"を探したいのですか? – Eddy