7

我有一个名为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'上得到空值

4

8 回答 8

7

试用

linq到xml的方式

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;

}
于 2012-11-28T12:48:51.020 回答
1

如果您想在这种情况下获取“目录”标签的值,请使用它作为简短语法:

var directory = infodoc.GetElementsByTagName("directory")[0].Attributes["value"].Value;
于 2021-07-02T01:47:19.013 回答
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 ?? "");
}
于 2015-05-01T19:55:57.657 回答
0
if (xml.NodeType == XmlNodeType.Element && xml.Name == "Architecture")
{
    string architecture = xml.ReadElementContentAsString();
}
于 2014-12-15T13:02:56.370 回答
0

因为您没有 ID 为“目录”的元素。无论你想要

GetElementByID("backUpPath").GetAttribute("value");

或者

GetElementsByTagName("directory");

请记住,第二个方法返回一个 XMLNodeList!

于 2012-11-28T12:49:37.027 回答
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"];

            }
于 2012-11-28T12:50:59.450 回答
0
XmlDocument infodoc = new XmlDocument();
infodoc.Load("BackupManager.xml");
XmlElement directoryElement = document.GetElementById("directory");
string backupPath = directoryElement.GetAttribute("value");
于 2012-11-28T12:53:54.680 回答
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);

            }
于 2017-09-27T04:32:01.287 回答