我有一个名为BackupManager.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Settings>
<directory id="backUpPath" value="D:/BACKUPS/"></directory>
<filename id="feilname" value="SameName"></filename>
<period id ="period" value="15"></period>
</Settings>
</configuration>
我试图从标签中获取值到字符串 例如: - 我需要'backUpPath'标签的值为'D:/ BACKUPS /'到一个字符串'str'
我试过的代码是
XmlDocument infodoc = new XmlDocument();
infodoc.Load("BackupManager.xml");
//int col = infodoc.GetElementsByTagName("directory").Count;
String str = infodoc.GetElementByID("directory").value;
但是我在'str'
上获得了空值答案 0 :(得分:6)
试试
linq to xml way
IEnumerable<XElement> direclty = infodoc.Elements("Settings").Elements("directory");
var rosterUserIds = direclty .Select(r => r.Attribute("value").Value);
或强>
XmlNodeList nodeList=
(infodoc.SelectNodes("configuration/Settings/directory"));
foreach (XmlNode elem in nodeList)
{
string strValue = elem.Attributes[1].Value;
}
答案 1 :(得分:0)
因为您没有ID为“directory”的元素。你要么
GetElementByID("backUpPath").GetAttribute("value");
或者
GetElementsByTagName("directory");
请记住,第二种方法返回XMLNodeList!
答案 2 :(得分:0)
如果您愿意,可以使用XmlReader
string str ="";
using (var reader = new StreamReader(BackupManager.xml))
{
var all = reader.ReadToEnd();
StringReader stringReader = new StringReader(all);
XmlReader xmlReader = XmlTextReader.Create(stringReader,new System.Xml.XmlReaderSettings() { IgnoreWhitespace = true, IgnoreComments = true });
while (xmlReader.Read())
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "directory")
str = xmlReader["value"];
}
答案 3 :(得分:0)
XmlDocument infodoc = new XmlDocument();
infodoc.Load("BackupManager.xml");
XmlElement directoryElement = document.GetElementById("directory");
string backupPath = directoryElement.GetAttribute("value");
答案 4 :(得分:0)
if (xml.NodeType == XmlNodeType.Element && xml.Name == "Architecture")
{
string architecture = xml.ReadElementContentAsString();
}
答案 5 :(得分:0)
在过去,我不得不处理一个巨大的XML,性能也是问题所在。我所需要的只是对XML的非缓存,仅向前,只读访问。
此外,我没有控制架构,只需要从XML和也CDATA 中挤出某些标记值。
以下是我最终使用的内容:
private string GetValueFromXmlTag(string xml, string tag)
{
if (xml == null || tag == null || xml.Length == 0 || tag.Length == 0)
return "";
string
startTag = "<" + tag + ">",
endTag = "</" + tag + ">",
value = null;
int
startTagIndex = xml.IndexOf(tag, StringComparison.OrdinalIgnoreCase),
endTagIndex = xml.IndexOf(endTag, StringComparison.OrdinalIgnoreCase);
if (startTagIndex < 0 || endTagIndex < 0)
return "";
int valueIndex = startTagIndex += startTag.Length - 1;
try
{
value = xml.Substring(valueIndex, endTagIndex - valueIndex);
}
catch (ArgumentOutOfRangeException responseXmlParserEx)
{
string err = string.Format("Error reading value for \"{0}\" tag from XXX XML", tag);
log.Error(err, responseXmlParserEx);
}
return (value ?? "");
}
答案 6 :(得分:0)
XmlDocument infodoc = new XmlDocument();
//Server.MapPath() return the xml file address
infodoc.Load(Server.MapPath("~/XMLFile1.xml"));
XmlNodeList nodeList =
(infodoc.SelectNodes("configuration/Settings/backUPpath"));
foreach (XmlNode elem in nodeList)
{
Response.Write(elem.Attributes[1].Value);
}
答案 7 :(得分:0)
如果您想在这种情况下获取 'directory' 标记的值,请使用它作为简短的语法:
var directory = infodoc.GetElementsByTagName("directory")[0].Attributes["value"].Value;