Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XmlDocument读取XML属性_C#_.net_Xml_Xmldocument - Fatal编程技术网

C# 使用XmlDocument读取XML属性

C# 使用XmlDocument读取XML属性,c#,.net,xml,xmldocument,C#,.net,Xml,Xmldocument,如何使用C#的XmlDocument读取XML属性 我有一个XML文件,看起来有点像这样: <?xml version="1.0" encoding="utf-8" ?> <MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream"> <Other stuff /> </MyConfiguration>

如何使用C#的XmlDocument读取XML属性

我有一个XML文件,看起来有点像这样:

<?xml version="1.0" encoding="utf-8" ?>
<MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream">
    <Other stuff />
</MyConfiguration> 

如何读取XML属性SuperNumber和SuperString


目前我正在使用XmlDocument,我使用XmlDocument的
GetElementsByTagName()
获得介于两者之间的值,这非常有效。我就是不知道如何获取属性?

XmlDocument.attributes
也许?(它有一个方法GetNamedItem,虽然我一直在迭代属性集合,但它可能会执行您想要的操作)

XmlNodeList elemList=doc.GetElementsByTagName(…);
for(int i=0;i
您应该查看。一旦您开始使用它,您会发现它比遍历列表更高效、更容易编码。它还可以让你直接得到你想要的东西

那么代码将类似于

string attrVal = doc.SelectSingleNode("/MyConfiguration/@SuperNumber").Value;

请注意,XPath 3.0于2014年4月8日成为W3C的推荐标准。

您可以迁移到XDocument而不是XmlDocument,然后如果您喜欢这种语法,可以使用Linq。比如:

var q = (from myConfig in xDoc.Elements("MyConfiguration")
         select myConfig.Attribute("SuperString").Value)
         .First();

我有一个Xml文件books.Xml

<ParameterDBConfig>
    <ID Definition="1" />
</ParameterDBConfig>

节目:

XmlDocument doc = new XmlDocument();
doc.Load("D:/siva/books.xml");
XmlNodeList elemList = doc.GetElementsByTagName("ID");     
for (int i = 0; i < elemList.Count; i++)     
{
    string attrVal = elemList[i].Attributes["Definition"].Value;
}
XmlDocument doc=新的XmlDocument();
doc.Load(“D:/siva/books.xml”);
XmlNodeList elemList=doc.GetElementsByTagName(“ID”);
for(int i=0;i

现在,
attrVal
的值为
ID

假设示例文档位于字符串变量
doc

> XDocument.Parse(doc).Root.Attribute("SuperNumber")
1

如果XML包含名称空间,则可以执行以下操作以获取属性值:

var xmlDoc = new XmlDocument();

// content is your XML as string
xmlDoc.LoadXml(content);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

// make sure the namespace identifier, URN in this case, matches what you have in your XML 
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol");

// get the value of Destination attribute from within the Response node with a prefix who's identifier is "urn:oasis:names:tc:SAML:2.0:protocol" using XPath
var str = xmlDoc.SelectSingleNode("/ns:Response/@Destination", nsmgr);
if (str != null)
{
    Console.WriteLine(str.Value);
}

有关XML名称空间和的详细信息。

非常感谢。它真的很有效,不需要任何路径,什么都不需要。简直太棒了!!
var xmlDoc = new XmlDocument();

// content is your XML as string
xmlDoc.LoadXml(content);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

// make sure the namespace identifier, URN in this case, matches what you have in your XML 
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol");

// get the value of Destination attribute from within the Response node with a prefix who's identifier is "urn:oasis:names:tc:SAML:2.0:protocol" using XPath
var str = xmlDoc.SelectSingleNode("/ns:Response/@Destination", nsmgr);
if (str != null)
{
    Console.WriteLine(str.Value);
}