从文件

时间:2015-07-14 06:47:48

标签: c# xml

我从xml文件中读取信息时出了点问题......

传递给我的文件有数千行。我只对300到400行感兴趣。当用户完成其操作并且要读取的数据可以存储在List<string>中时,我不需要将任何数据写回xml。

我使用XmlTextReader在网上找到解决方案来读取数据。所以我不必创建一个类并使用Serializer。但似乎我使用XmlTextReader错误。也许你可以帮助我...

这就是xml的样子:

<?xml version="1.0" encoding="utf-8"?>
<ProjectConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ProjectLists xmlns="...">
    <ProjectList>
  ... // not interested in this data
   </ProjectList>
  <ProjectList>
  <ListNode>
    <Name>Test_Environment</Name>
    <Children>
      <ListNode>
        <Name>yyy</Name>
        <Children>
          <ListNode>
            <Name>205 (ST)</Name>
            <Children>
              <ListNode>
                <Name>098-0031</Name>
                <Children />
              </ListNode>
              <ListNode>
                <Name>098-0032</Name>
                <Children />
              </ListNode>
              //more ListNodes...
            </Children>
          </ListNode>
          <ListNode>
            <Name>old</Name>
            <Children>
              <ListNode>
                <Name>W098-32</Name>
                <Children />
              </ListNode>
            </Children>
          </ListNode>
        </Children>
      </ListNode>
      <ListNode>
        <Name>xxx</Name>
        <Children />
      </ListNode>
      <ListNode>
        <Name>098-0001</Name>
        <Children />
      </ListNode>
      <ListNode>
        <Name>098-0011</Name>
        <Children />
      </ListNode>
      // More List Nodes
    </Children>
  </ListNode>
  <ListNode>
    // more List Nodes
  </ListNode>
</ProjectList>
<ProjectList>
  //more uninteresting ProjectLists...
</ProjectList>

我只对最内部名称元素的Value感兴趣(前两个将是&#34; 098-0031&#34;&#34; 098-0032&#34; )。

这是我的代码:

while (reader.Read()) {
            switch (reader.NodeType) {
                case XmlNodeType.Element:
                    {
                        if (reader.Name == "Name") {
                            reader.Read();
                            if (reader.Value == "Test_Environment") {
                                reader.ReadToDescendant("Children");
                                if (reader.Name == "Children") {
                                    reader.ReadToDescendant("Children");

                                }
                            }
                        }
                    }
                    break;
            }
        }

但条件reader.Name == "Children"永远不会满满的......有人可以向我解释原因。也许告诉我一个简单的方法来将这些值存储在List<string>中?提前谢谢!

编辑:我编辑了xml。对不起,但很难从我的xml过滤掉不必要的混乱部分...

5 个答案:

答案 0 :(得分:2)

static void GetMostInnerName()
{
    string xml = @"<ProjectConfiguration xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<ProjectLists>   
<ProjectList>
<ListNode>
<Name>Test_Environment</Name>
<Children>
<ListNode>
<Name>yyy</Name>
<Children>
<ListNode>
<Name>205 (ST)</Name>
<Children>
<ListNode>
<Name>098-0031</Name>
<Children />
</ListNode>
<ListNode>
<Name>098-0032</Name>
<Children />
</ListNode>
</Children>
</ListNode>
<ListNode>
<Name>old</Name>
<Children>
    <ListNode>
    <Name>W098-32</Name>
    <Children />
    </ListNode>
</Children>
</ListNode>
</Children>
</ListNode>
<ListNode>
<Name>xxx</Name>
<Children>
<ListNode>
<Name>098-0001</Name>
<Children />
</ListNode>
<ListNode>
<Name>098-0011</Name>
<Children />
</ListNode>
</Children>
</ListNode>
// more List Nodes
</Children>
</ListNode>
</ProjectList></ProjectLists>
</ProjectConfiguration>";
    XElement root = XElement.Parse(xml).Element("ProjectLists");
    //var xmlns = root.GetDefaultNamespace();
    //Console.WriteLine(xmlns);
    var eles = root.Elements("ProjectList").SelectMany(x => x.Elements("ListNode"));
    List<string> list = new List<string>();
    foreach (var ele in eles)
    {
        Loop(ele, list);
    }
    list.ForEach(x =>
    {
        Console.WriteLine(x);
    });
}
static void Loop(XElement ele, List<string> list)
{
    var child = ele.Element("Children");
    if (child != null && child.HasElements)
    {
        foreach (var e in child.Elements("ListNode"))
        {
            Loop(e, list);
        }
    }
    else
    {
        list.Add(ele.Element("Name").Value);
    }
}

因为你的xml有很多像ProjectList这样的节点,所以我在这里使用SelectMany,并添加root元素进行测试,最后一个输出是

098-0031
098-0032
W098-32
098-0001
098-0011

答案 1 :(得分:1)

private static void FillNames(XElement container, List<string> result)
{
    XElement[] listNodes = container.Elements("ListNode").ToArray();
    if (!listNodes.Any())
        return;

    foreach (XElement listNode in listNodes)
    {
        XElement nameElement = listNode.Element("Name");
        if (nameElement == null)
            continue;

        XElement childrenElement = listNode.Element("Children");
        if (childrenElement == null)
            continue;

        if (!childrenElement.HasElements)
            result.Add(nameElement.Value);
        else
            FillNames(childrenElement, result);
    }
}

static void Main(string[] args)
{
    var result = new List<string>();

    string xml = Resources.Xml; // TODO: put your xml here

    XDocument doc = XDocument.Parse(xml);
    if (doc.Root == null)
        return;

    XElement[] projects = doc.Root.Elements("ProjectList").ToArray();

    foreach (XElement project in projects)
        FillNames(project, result);
}

答案 2 :(得分:0)

这有用吗?:)

                string path = @"path.xml";

                   if(File.Exists(path)){
                XmlTextReader reader = new XmlTextReader(path);

                Console.WriteLine("");
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {

                        case XmlNodeType.Element:  
                            Console.Write("<" + reader.Name);
                            Console.WriteLine(">");
                            break;

                        case XmlNodeType.Text:   
                            Console.WriteLine(reader.Value);
                            break;
                        case XmlNodeType.EndElement: 
                            Console.Write("</" + reader.Name);
                            Console.WriteLine(">");
                            break;
                    }
                }
                Console.ReadLine();
                   }else{
                       Console.WriteLine("Error file not found");
                   }

答案 3 :(得分:0)

这是什么语言?您可以使用标记以类似于JSON的方式遍历XML。

答案 4 :(得分:0)

使用LinqToXml

var xDocument = XDocument.Parse("yourXmlString");
        XNamespace ns = xDocument.Root.GetDefaultNamespace();
        var result = (from e in xDocument
                          .Descendants(ns + "ListNode").Descendants(ns + "Children")
                          .Descendants(ns + "ListNode").Descendants(ns + "Children")
                          .Descendants(ns + "ListNode").Descendants(ns + "Children")
                          .Descendants(ns + "ListNode")
                           select e.Element(ns + "Name").Value).ToList();