English 中文(简体)
• 为儿童提供食肉和C#
原标题:Get Children as string with xmlreader and C#

I am parsing an xml string using xmlreader in c#, but as i parse i need sometimes to get the full content of a node including children with tags and still be able to continue parsing.

ReadInnerXML和ReteOutterXML打破了我所有的大门。

example XML:

<?xml version="1.0" standalone="yes"?>
<main>
    <parse1> //finding this will get full inner or outter xml - either one - my issue
         <parse2 /> // even getting the children above might still need parse chldren
         <negligeable /> // not all children interest me
   </parse1>
   <parse3>some text</parse3> // not all children of main are the same but all need be parsed
</main>

希望使大家对需要做什么有一个普遍的想法。

i 现在可以把2和3同起来,忽视不需要什么,但如果在发现标签时使用“读式”或“读式”法,那么就让我放弃任何其他东西——甚至连帽子都不在。

ReadInnerXML and ReadOutterXML do return the text i need correctly but cause everything else to not be parsed

<>strong>EDIT: as per dasblinkenlight sugestion, some Code:

using (XmlReader reader = XmlReader.Create(new StringReader(XmlString)))
{
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                switch (reader.Name)
                {
                    case "parse1":
                         Console.WriteLine("Contents of Parse 1: {0}", ?function here?);
                         break;
                    case "parse2":
                         Console.WriteLine("Parse 2 tag exists");
                         break;
                    case "parse3":
                         Console.WriteLine("Contents of Parse 3: {0}", Reader.ReadElementContentAsString());
                         break;
                }
                break;
        }
    }
}

成果应当是(经过测试xml)

Contents of Parse 1: <parse2 /><negligeable />
Parse 2 tag exists
Contents of Parse 3: some text

Am 并试图阅读

任何方面?

最佳回答

Basically readinnerxml is reading all the way to the end and XmlReader is forward only. You might get away with XmlDocument, or another way, would be to create another reader from the same Xml content, read to where you are in the orginal, get your string and bin the copy

问题回答

使用<代码>XmlDocument 您可以很容易地通过您的xml元素和印刷你想要的东西。

例如:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlString);

string parse1_Content = xmlDocument.GetElementsByTagName("parse1")[0].InnerXml;
Console.WriteLine("Contents of Parse 1: " + parse1_Content);

if(xmlDocument.GetElementsByTagName("parse2") > 0)
    Console.WriteLine("Parse 2 exists");

string parse3_Content = xmlDocument.GetElementsByTagName("parse1")[0].InnerText;
Console.WriteLine(parse3_Content);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签