private static void ParseXmlNode(Story story, XmlNode xmlNode, LocationNode parentNode, List <Location> locations) { //int id = int.Parse(xmlNode.Attributes["Id"].Value); string name = xmlNode.Attributes["Name"] != null ? xmlNode.Attributes["Name"].Value : "Unnamed"; string color = xmlNode.Attributes["Color"] != null ? xmlNode.Attributes["Color"].Value : "Black"; bool visible = xmlNode.Attributes["Visible"] != null && bool.Parse(xmlNode.Attributes["Visible"].Value); string list = xmlNode.Attributes["Sessions"] != null ? xmlNode.Attributes["Sessions"].Value : ""; Location location = new Location(name, color, visible, parentNode.Id); int locationId = locations.Count; string[] items = list.Split(','); foreach (string item in items) { if (!string.IsNullOrWhiteSpace(item)) { int sessionId = int.Parse(item.Trim()); location.Sessions.Add(sessionId); story._sessionToLocation.Add(sessionId, locationId); } } locations.Add(location); LocationNode curNode = new LocationNode(locationId); parentNode.Children.Add(curNode); foreach (XmlNode child in xmlNode.ChildNodes) { ParseXmlNode(story, child, curNode, locations); } }
private static void ParseXmlNode(XmlNode xmlNode, LocationNode parentNode) { int id = xmlNode.Attributes["Id"] != null?int.Parse(xmlNode.Attributes["Id"].Value) : -1; string name = xmlNode.Attributes["Name"] != null ? xmlNode.Attributes["Name"].Value : "Unnamed"; string color = xmlNode.Attributes["Color"] != null ? xmlNode.Attributes["Color"].Value : "Black"; bool visible = xmlNode.Attributes["Visible"] != null && bool.Parse(xmlNode.Attributes["Visible"].Value); string list = xmlNode.Attributes["Sessions"] != null ? xmlNode.Attributes["Sessions"].Value : ""; var location = new LocationNode(id, name, color, visible, parentNode); string[] items = list.Split(','); foreach (string item in items) { if (!string.IsNullOrWhiteSpace(item)) { int sessionId = int.Parse(item.Trim()); location.Sessions.Add(sessionId); } } parentNode.Children.Add(location); foreach (XmlNode child in xmlNode.ChildNodes) { ParseXmlNode(child, location); } }
public LocationNode(int id, string name, string color, bool visible, LocationNode parent) { Name = name; Visible = visible; Sessions = new List <int>(); Parent = parent; Id = id; Children = new List <LocationNode>(); var convertFromString = ColorConverter.ConvertFromString(color); if (convertFromString != null) { Color = (Color)convertFromString; } }
public static lstory Read(string path) { lstory lstory = new lstory(); lstory.FileName = path; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); // 1.read locations and tree structure XmlNode xmlNode = xmlDoc.SelectSingleNode("Story/Locations"); lstory.LTree = new LocationNode(-1, "root", "Black", false, null); if (xmlNode != null && xmlNode.HasChildNodes) { foreach (XmlNode locationNode in xmlNode.ChildNodes) { ParseXmlNode(locationNode, lstory.LTree); } } // 2.read characters and session table lstory.Characters = new List <Character>(); lstory.segments = new List <List <segmentread> >(); HashSet <int> timestamps = new HashSet <int>(); lstory.sessionCount = 0; xmlNode = xmlDoc.SelectSingleNode("Story/Characters"); foreach (XmlNode characterNode in xmlNode.ChildNodes) { string name = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Name"].Value : ""; string color = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Color"].Value : "Black"; double weight = characterNode.Attributes["Weight"] != null?double.Parse(characterNode.Attributes["Weight"].Value) : 1.0; Character character = new Character(name, weight, color, lstory.Characters.Count); lstory.Characters.Add(character); lstory.segments.Add(new List <segmentread>()); foreach (XmlNode spanNode in characterNode.ChildNodes) { int start = int.Parse(spanNode.Attributes["Start"].Value); int end = int.Parse(spanNode.Attributes["End"].Value); timestamps.Add(start); timestamps.Add(end); string[] prevItems = spanNode.Attributes["Prev"].Value.Split(','); var prev = prevItems.Select(item => int.Parse(item.Trim())).ToList(); int now = int.Parse(spanNode.Attributes["Now"].Value); lstory.sessionCount = now > lstory.sessionCount ? now : lstory.sessionCount; string[] nextItems = spanNode.Attributes["Next"].Value.Split(','); var next = nextItems.Select(item => int.Parse(item.Trim())).ToList(); double sweight = double.Parse(spanNode.Attributes["Weight"].Value); lstory.segments.Last().Add(new segmentread(start, end, prev, now, next, sweight)); } } lstory.TimeStamps = timestamps.ToArray(); Array.Sort(lstory.TimeStamps); lstory.FC = lstory.TimeStamps.Count() - 1; lstory.TimeStampStartDic = new Dictionary <int, int>(); lstory.TimeStampEndDic = new Dictionary <int, int>(); for (int i = 0; i < lstory.TimeStamps.Length - 1; i++) { lstory.TimeStampStartDic.Add(lstory.TimeStamps[i], i); lstory.TimeStampEndDic.Add(lstory.TimeStamps[i + 1], i); } lstory.sessionCount++; return(lstory); }
public static lstory ReadOriginalXml(string path) { lstory lstory = new lstory(); lstory.FileName = path; List <string> getdata = new List <string>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); // 1.read locations and tree structure XmlNode xmlNode = xmlDoc.SelectSingleNode("Story/Locations"); lstory.LTree = new LocationNode(-1, "root", "Black", false, null); if (xmlNode != null && xmlNode.HasChildNodes) { foreach (XmlNode locationnode in xmlNode.ChildNodes) { ParseXmlNode(locationnode, lstory.LTree); } } // 2.read characters and session table lstory.Characters = new List <Character>(); lstory.segments = new List <List <segmentread> >(); HashSet <int> timestamps = new HashSet <int>(); lstory.sessionCount = 0; xmlNode = xmlDoc.SelectSingleNode("Story/Characters"); foreach (XmlNode characterNode in xmlNode.ChildNodes) { string name = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Name"].Value : ""; getdata.Add(name); string color = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Color"].Value : "Black"; double weight = characterNode.Attributes["Weight"] != null?double.Parse(characterNode.Attributes["Weight"].Value) : 1.0; Character character = new Character(name, weight, color, lstory.Characters.Count); lstory.Characters.Add(character); lstory.segments.Add(new List <segmentread>()); foreach (XmlNode spanNode in characterNode.ChildNodes) { int start = int.Parse(spanNode.Attributes["Start"].Value); getdata.Add(Convert.ToString(start)); int end = int.Parse(spanNode.Attributes["End"].Value); getdata.Add(Convert.ToString(start)); timestamps.Add(start); timestamps.Add(end); int now = int.Parse(spanNode.Attributes["Session"].Value); lstory.sessionCount = now > lstory.sessionCount ? now : lstory.sessionCount; var prev = new List <int>(); var next = new List <int>(); double sweight = 1.0; lstory.segments.Last().Add(new segmentread(start, end, prev, now, next, sweight)); } } lstory.TimeStamps = timestamps.ToArray(); for (int i = 0; i < lstory.Characters.Count; i++) { if (lstory.segments[i].Count == 1) { lstory.segments[i][0].Prev.Add(-1); lstory.segments[i][0].Next.Add(-1); } else { #region >=2 int j = 0; lstory.segments[i][j].Prev.Add(-1); if (lstory.segments[i][j + 1].Start == lstory.segments[i][j].End) { lstory.segments[i][j].Next.Add(lstory.segments[i][j + 1].Now); } for (j = 1; j < lstory.segments[i].Count - 1; j++) { if (lstory.segments[i][j].Start == lstory.segments[i][j - 1].End) { lstory.segments[i][j].Prev.Add(lstory.segments[i][j - 1].Now); } if (lstory.segments[i][j].End == lstory.segments[i][j + 1].Start) { lstory.segments[i][j].Next.Add(lstory.segments[i][j + 1].Now); } } lstory.segments[i][j].Next.Add(-1); if (lstory.segments[i][j].Start == lstory.segments[i][j - 1].End) { lstory.segments[i][j].Prev.Add(lstory.segments[i][j - 1].Now); } #endregion } } Array.Sort(lstory.TimeStamps); lstory.FC = lstory.TimeStamps.Count() - 1; getdata.Add(Convert.ToString(lstory.FC)); lstory.TimeStampStartDic = new Dictionary <int, int>(); lstory.TimeStampEndDic = new Dictionary <int, int>(); for (int i = 0; i < lstory.TimeStamps.Length - 1; i++) { lstory.TimeStampStartDic.Add(lstory.TimeStamps[i], i); lstory.TimeStampEndDic.Add(lstory.TimeStamps[i + 1], i); } lstory.sessionCount++; return(lstory); }
public Location(LocationNode node) { var loc = new Location(node.Name, node.Color.ToString(), node.Visible, node.Parent.Id); loc.Sessions = node.Sessions; }