c# XML Parsing将innerxml与innertext分离

发布于 2024-12-24 17:24:04 字数 1736 浏览 2 评论 0原文

我想要做的是理想情况下创建一个嵌套列表,基本上是一个二维列表,或者一个二维数组(如果这更适合此任务),其工作原理如下: 1 名称 => Hickory 无需显式选择节点。

我可以使用 SelectNode (Woods/Wood),然后执行类似 node["ID"].InnerText 的操作,但这需要我知道节点名称是什么。

假设即使有 36 个节点而不是 7 个,它也会读取 wood.xml 并且我永远不会知道节点的名称。我尝试使用 outerxml/innerxml 但这给了我太多信息。

 XmlDocument doc = new XmlDocument();
        doc.Load("wood.xml");

        //Here is wood.xml
        /*<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>*/


        XmlNode root = doc.FirstChild;

        //Display the contents of the child nodes.
        if (root.HasChildNodes)
        {
            for (int i=0; i<root.ChildNodes.Count; i++)
            {
                Console.WriteLine(root.ChildNodes[i].InnerXml);
                Console.WriteLine();
            }

            Console.ReadKey();
        }

如果你愿意的话,我基本上可以创建一个木质“缓冲区”,这样我就可以在其他地方访问这些值。

抱歉,如果我不清楚,我想本质上使这个“抽象”,因为缺乏更好的词。

因此,如果有一天我将“Weight”的名称更改为“HowHeavy”,或者添加一个附加元素“NumberOfBranches”,我就不必对 xml 文件的结构进行硬编码。

What I am trying to do is create ideally a nested List basically a 2d list, or a 2D array if that is better for this task, that would work as follows ID => 1 Name => Hickory without explicitly selecting the node.

I could use SelectNode (Woods/Wood) and then do something like node["ID"].InnerText but that would require that I know what the nodes name is.

Assume that this would read wood.xml even if there were 36 nodes instead of 7 and that I will never know the name of the nodes. I tried using outerxml/innerxml but that gives me too much information.

 XmlDocument doc = new XmlDocument();
        doc.Load("wood.xml");

        //Here is wood.xml
        /*<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>*/


        XmlNode root = doc.FirstChild;

        //Display the contents of the child nodes.
        if (root.HasChildNodes)
        {
            for (int i=0; i<root.ChildNodes.Count; i++)
            {
                Console.WriteLine(root.ChildNodes[i].InnerXml);
                Console.WriteLine();
            }

            Console.ReadKey();
        }

That would allow me to basically create a wood "buffer" if you will so I can access these values elsewhere.

Sorry if I was unclear I want to essentially make this "abstract" for lack of a better word.

So that if I were someday to change the name of "Weight" to "HowHeavy" or if i were to add an additional element "NumberOfBranches" I would not have to hardcode the structure of the xml file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-12-31 17:24:04

这就是你所追求的吗?

class Program
    {   
        static void Main(string[] args)
        {
          string xml = @"<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>";

           XDocument doc = XDocument.Parse(xml);
           //Get your wood nodes and values in a list 
           List<Tuple<string,string>> list = doc.Descendants().Select(a=> new Tuple<string,string>(a.Name.LocalName,a.Value)).ToList();

           // display the list
           list.All(a => { Console.WriteLine(string.Format("Node name {0} , Node Value {1}", a.Item1, a.Item2)); return true; });
           Console.Read();
        }
    }  

Is this what you after ?

class Program
    {   
        static void Main(string[] args)
        {
          string xml = @"<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>";

           XDocument doc = XDocument.Parse(xml);
           //Get your wood nodes and values in a list 
           List<Tuple<string,string>> list = doc.Descendants().Select(a=> new Tuple<string,string>(a.Name.LocalName,a.Value)).ToList();

           // display the list
           list.All(a => { Console.WriteLine(string.Format("Node name {0} , Node Value {1}", a.Item1, a.Item2)); return true; });
           Console.Read();
        }
    }  
勿忘心安 2024-12-31 17:24:04

您可以使用 xmlDocument.SelectNodes("//child::node()")

You can use xmlDocument.SelectNodes("//child::node()")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文