I am trying to read a certain section from an xml file in C#. I tried using this code here but I get a compiler error under the Text
in XmlNodeType.Text
but the weird thing is it comes up with intellisense and gives the same error with everything else like Element
,Comment
etc.. what am I missing?
XmlTextReader reader = new XmlTextReader(xmlDoc);
List<string> paths = new List<string>();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Paths")
foreach(XmlNodeType.Text aa in reader.ReadInnerXml())
paths.Add(aa);
}
reader.Close();
XML file
<Config>
<Paths>
<Input>C:\</Input>
<Output>C:\</Output>
<Log>\Logs</Log>
</Paths>
<SystemOwnerRoles>
<Supplier>SUPPLIER</Supplier>
<Mop>MOP</Mop>
</SystemOwnerRoles>
</Config>
XmlNodeType
is an enum. XmlNodeType.Text
is a value, not a type, but you're trying to use it as the type of the aa
variable. Furthermore ReaderInnerXml()
returns a string
, so it's not clear how you expect to iterate over it.
Do you have to use XmlTextReader
for this? Almost all XML work is simpler using LINQ to XML. For example, this is all I think you need:
var paths = XDocument.Load(xmlDoc)
.Descendants("Paths")
.Elements()
.Select(element => (string) element)
.ToList();
See more on this question at Stackoverflow