简体   繁体   中英

How to get the value for multiple subnode in xml?

  1. Xml code:

      <Report> <ChartData> <ListName>area</ListName> <ViewName>Selecte List</ViewName> <YAxisFields> <YAxisField> <Name>Scheduled Start Date/Time</Name> <DataType>DateTime</DataType> <Category>Year</Category> </YAxisField> </YAxisFields> <XAxisFields> <XAxisField> <Name>Release Type</Name> <DataType>String</DataType> <Category> </Category> </XAxisField> </XAxisFields> </ChartConfig> </Report> 
  2. I got the value for the subnode listname and viewname by using the below code,

      XmlDocument doc = new XmlDocument(); doc.Load("XmlFileName"); XmlNodeList node = doc.SelectNodes("Report/ChartData"); foreach (XmlNode xn in node) { xn["ListName"].InnerXml = chartname; xn["ViewName"].InnerXml = SelectedList; **xn["YAxisFields/YAxisField"].InnerXml = yaxisfield; //not working, need to get the value for this xml node,need help in this line dono how to proceed** doc.Save("XmlFilename"); } 
  3. First i have tried with code like this instead of above code,in this i need to create number of objects in order get the value for each node so i tried by creating object for xmlnodelist then i used foreach loop to get the value for each node but in this couldnt get the value for YAxisFields/YAxisField because it also has parent node as YAxisFields and subnode as YAxisField so there is only way to create number of objects for xmlnode or is there any other way to do this?

      XmlDocument doc = new XmlDocument(); doc.Load("XmlFileName"); XmlNode Listnode = doc.SelectSingleNode("Report/ChartData/ListName"); XmlNode Viewnode = doc.SelectSingleNode("Report/ChartData/ViewName"); if (Listnode != null) { Listnode.InnerXml = chartname; Viewnode.InnerXml = SelectedList; ; doc.Save("XmlFileName"); 

Use Linq to XML XDocument, like this:

doc.Root.Descendants("ChartData").ToList().ForEach(node =>
                {
                    node.Element("ListName").Value = chartname;
                    node.Element("ViewName").Value = SelectedList;
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM