私はC#を使用して次のXMLを読みたいと思います。C#を使用してxmlns属性を含むxmlノードを読み取る
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<object></object>
</objects>
</spring>
</configuration>
オブジェクトノードからxmlnsを識別することはありません。
string xmlFile = @"App1.config";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNodeList nodeList = xmlDoc.SelectNodes("configuration/spring/objects/object");
foreach (XmlElement xmlElement in nodeList)
{
string id = xmlElement.GetAttribute("id"); //Output - SimulatorControlTCP
XmlNodeList xmlPropertyNodeList = xmlElement.SelectNodes("property");
foreach (XmlElement xmlPropertyElement in xmlPropertyNodeList)
{
id = xmlPropertyElement.GetAttribute("value");
if ((id.Contains("tcp") || id.Contains("http")) && id.Contains("localhost"))
{
id = id.Replace("localhost","1.1.1.1");
xmlPropertyElement.Attributes[1].Value = id;
xmlDoc.Save(xmlFile);
}
}
}
foreachループには入りません。 xmlnsを削除すると、上記のコードは正常に動作します。
XmlDocument.Readを呼び出した後、 "objects"という名前のXmlNodeが取得されますが、空のXmlAttributeCollection? –
私はコードを追加しました。コードはxmlns属性を削除した後に動作します – user660232
xmlns属性が含まれているため、XmlDocument.Readを呼び出した後でオブジェクトノードを取得できません – user660232